Friday, February 22, 2019

File Exception Handling and Pickling in Python


Two handy tools every Python developer should know about are handling file exceptions and pickling. While not directly related, they help generate more user-friendly error messages and aid in the timely processing of large data sets, respectively.

File Handling

Whenever there is a possibility that a user could receive an error while a script is processing, it is a good idea to look into rewriting the error message so that it is more user-friendly. For example, this code returns the following computer-generated error when the file ‘cat.txt’ does not exist:

objFile = open("cat.txt", "r")
print("File opened.")

returns

FileNotFoundError: [Errno 2] No such file or directory: 'cat.txt'

In all fairness, this is a fairly user-friendly computer-generated error as it is, however, I still think it could be a bit clearer. To do this, I rewrite the code to include a try-except block:

#attempt to open the cat.txt file
try:
    objFile =
open("cat.txt", "r")
   
print("File opened.")

#report error if cat.txt file does not exist
except:
   
print("The file you are attempting to open does not exist.")


Now, when the file ‘cat.txt’ does not exist, the following error is returned:

The file you are attempting to open does not exist.

While the message is fairly similar to the original, computer-generated error, it reads much clearer; there is less extraneous information to have to navigate.

Another, more sophisticated, error handling technique that can be invoked makes use of  the built-in Python class, Exception. For example, here is some code that modifies the ZeroDivisionError computer-generated error message:

try:
    decValue = 5/0
    print(decValue)
except ZeroDivisionError as e:
    print("do not divide by zero!")
code from Randal Root, Mod7PythonProgrammingNotes.doc lecture notes, Feb 2019

Here, is what the computer-generated error message looks like without the try-except block:

ZeroDivisionError: division by zero

Compare this to the error message in the try-except block:

do not divide by zero!

Much more intuitive, right? Here, the code is making use of one of the “child” class objects of the built-in Python class, Exception. The computer has a number of these that can be modified to read in a more straightforward way (IOError, IndexError, KeyError, NameError, SyntaxError,TypeError,ValueError). The script doesn’t have to make use of just one either; it can invoke multiple except blocks to handle any combination of these errors.

Another tool that can be used in Python is called pickling.

Pickling

As author Michael Dawson expresses in his book, Python Programming for the Absolute Beginner (2010),

Sometimes you may want to store more complex information, like a list or a dictionary, for example. You could try to convert the contents of these data structures to characters and save them to a text file, but Python offers a much better way [called pickling]. You can store more complex data in a file with a single line of code. (p. 200)

In pickling, the code is saved as a binary file, which, while nonsensical to try for the developer to read, is very computer-readable. So much so, in fact, that it can be processed by the computer “fifty to thousands of times faster than reading from a text file” (https://www.youtube.com/watch?v=2Tw39kZIbhs&t=372s, created May 2015, accessed Feb 2019).

To pickle, use the following steps:




























   1 .   Import the module pickle    
   2.    Create or import the data to pickle. This could be any number of python objects- a dictionary, a list, a variable, an object created from a class, etc. (https://www.youtube.com/watch?v=2Tw39kZIbhs&t=372s, created May 2015, accessed Feb 2019)
   3.     Pickle the data by opening a file to store the data in and dump the data into it.
   4.     Read the new data file and load the pickled data to the computer.
   5.     Print the pickled data or other further processing (if desired).

As mentioned, pickling allows for much faster processing of large sets of data that can be stored and retrieved/reused later.

Creating custom script errors and pickling are two useful tools in the Python toolbox. The first makes it easier for users to understand when they encounter errors, the second improves processing time when chugging through large sets of data.

Complete scripts can be found in Assignment07.zip at:
https://github.com/deitesla/IntroToProgramming-Python