Monday, December 3, 2018


Brian Torretta
12/02/2018
IT FDN 100 B
Assignment 7

Exception Handling and Pickling examples

Introduction

The goal of this exercise is to show two simple examples: one for exception handling and another pickling, which is a (de)serialization module in a binary format. The examples, while trivial, are meant to showcase a specific functionality of error handling or pickling. I will also show and describe resources I found helpful outside of lecture notes and  Python® Programming for the Absolute Beginner

Note: source code can be found here: https://github.com/btorretta/IntroToProg-Python/blob/master/assignment7.zip

Pickling data

After previous exercises of reading and writing plaintext files, we were asked to show a simple example of using the pickle module.

As this isn't a built in module, we need to import it simply with a standard "import pickle" at the start of the example. Then we start our normal Data, Processing and Presentation flow, all of which are very brief in this example.

Data
Here we can show that the file handle variable is a string by using single quotes as opposed to another data type, like dictionary {}, etc. 

Presentation (and invoking the pickling function)

We ask the user for a file in string format, as a default of the input built in function and pass that named parameter to our processing function. Of course for that flow to happen the function needs to be called, which is what is executed first.


Processing:
Finally, we can process our data by attempting to load the file via pickle. We will incorporate a try except/except block as a method of testing to see if our file is in valid pickle binary format. We need to load up our filehandle passed by the presentation layer in a read binary attribute to the open method. I then attempt to load all its contents, so akin to read as opposed to readlines with respective to it's cursor position and return the variable to be fully encapsulated. I account for two error types in different sections, although technically they could be lumped together with a generic error message. File not found, a built in under the error class and under the pickle class the error we want if its not a pickled file.  Another error type I encountered during testing was an empty file, so while attempting to deserialize the data, it found nothing, which was unexpected and through an EOFerror.


Executing in PyCharm

I have a pre-created binary file from a different example file, not shown here to simply the example.


Executing in CLI
I have a pre-created plain text file


Additional helpful resources
   I referenced the official documentation for pickling: https://docs.python.org/3/library/pickle.html. In particular it helped me understand, with the aid of our TA, that pickling shines when there are efficiency gains dealing a data files that doesn't have a schema with its own packing/unpacking protocol. Also, when I received an error within the pickle class, stackoverflow came to the rescue: https://stackoverflow.com/questions/33307623/python-exception-safe-pickle-use.  


Another try/except example:
Following a similar scope to the previous example but without using pickling and targeting a different error type, I wrote code around the KeyboardInterrupt error. The data is an empty string to hold the keyboard input (kb) and presentation is simply a pretty printer. The processing asks for data for the parrot to echo unless you invoke a KeyboardInterrupt (like cntl c). This try except block includes a finally, which is always invoked after the try or the except.  



An interesting side effect of using the pycharm run mode is that control c is reserved (see https://stackoverflow.com/questions/39796689/why-doesnt-this-python-keyboard-interrupt-work-in-pycharm/39796898) 

  
Executing via cli (as mentioned above not possible via pycharm run to send an interrupt)



Additional helpful resources
I found that the official python documentation offered me exactly what I was looking for, a referential source of errors: https://docs.python.org/3/tutorial/errors.html. While I didn't implement it, it was helpful to know that you can assign the output of the error message to a variable and format it to your liking, typically like:
... except <error type> as err:
...     print('Handling run-time error:', err)
Conclusion
Identifying many of the error classes available and how to use flow control (try/except) to raise them was the source of this homework module. Additionally, leveraging the pickling module to (de)serialize data types to binary was useful from an efficiency standpoint. Finally, spending time on stackoverflow and the official documents proved a solid way to learn the basics of error handling and pickling.  

No comments:

Post a Comment