There are two small programs that I created this week. The first displays how error handling works in Python, and the second displays how pickling works. If you are interested in getting the program files themselves, feel free to visit
This Link to download the complete files with more detailed pseudo code than I'll be showing below
Error Handling:
When my python code encounters errors, it's nice to have python do something with those errors, so that the user isn't stuck with ugly jargon. You can re-code them to make those errors more user friendly and descriptive. This is accomplished using the following "try" and "except" syntax. I basically entered the code I wanted to execute after "try:"And I was able to customize the error message content after catching each error after the "except" blocks. Using the try/except basically allows the user and the program to encounter/experience errors more gracefully than if the program returned it's original error text.
Pickling:
When storing data in an external file, outside of your program. There are easier, and harder ways to save and extract data. If using a normal text file, you can store your data, row by row as string data. However, when using other python objects like lists, dictionaries, and tuples, it becomes a little bit more cumbersome to store this in a normal text file because it has to be converted into string data before it is stored, and when you want to read that data from the file, it must be parsed, cleansed, and then re-created as those object types within your code. Pickling allows you to store the python objects, as-is without any conversion & parsing. And when you want to read this pickled data from a file, you retrieve the actual object instead of string data that needs to be parsed & cleansed. In the example below, I decided to show the user that I could save a data dictionary of items ( i.e.
{"Fruit":"Apple","Vegetable":"Celery","Meat":"Chicken"}) to a file, and retrieve/unpickle it quite easily.
No comments:
Post a Comment