#-------------------------------------------------#
# Title: Python Error Handling & Pickle
# Dev: Craig Frost
# Date: December 3, 2018
# ChangeLog: (Craig Frost, 12/3/18, Initial Creation)
#-------------------------------------------------#
Github Repository: https://github.com/CraigFrost1/IntroToProg-Python/commit/9bcbdcd9c6114afc846221c4aeaccf3f7d5e5f04
Error handling in Python can be accomplished in several ways. The two types of error handling techniques I researched for this assignment are 'try and except' and using Python's built-in Exception handling. The article I found most helpful for my research was published by Python for Beginners (https://www.pythonforbeginners.com/error-handling/).
The error handling I chose to use in this assignment is 'try and except' error handling. According to Python for Beginners, there are three main components to 'try and except' error handling: try, except, and finally. "Try" allowed me to create a function and allow Python to try executing it. If the formula does not execute, I can leverage 'except' to instruct the application what to do (such as write the error to a log or display the error to the user). 'Finally' follows try and except and will execute whether or not the formula fails.
The script I created to test error handling is quite simple. It contains an IO class with two methods for getting data from the user (Figure 1). The user is asked to input two numbers that they would like to sum. It then advances to a processing class that contains the try and except formula. If the user attempts to add two items together that are integers, they will sum correctly and the result will be displayed. If the user attempts to add two items that are NOT integers, the following exception will be displayed "You did not enter a number for one of the variables!". (Figure 2)
Powered by TinyTake Screen Capture
Powered by TinyTake Screen Capture
# Title: Python Error Handling & Pickle
# Dev: Craig Frost
# Date: December 3, 2018
# ChangeLog: (Craig Frost, 12/3/18, Initial Creation)
#-------------------------------------------------#
Github Repository: https://github.com/CraigFrost1/IntroToProg-Python/commit/9bcbdcd9c6114afc846221c4aeaccf3f7d5e5f04
Python Error Handling and the 'Pickle' module
Introduction
In this module, we were introduced to two new concepts: error handling and using the pickle module. Python error handling is a way to capture errors as a program executes without causing the program to completely fail. The pickle module showed us how to import and use a module and how to leverage object sterilization for data storage and retrieval.
Python Error Handling
The error handling I chose to use in this assignment is 'try and except' error handling. According to Python for Beginners, there are three main components to 'try and except' error handling: try, except, and finally. "Try" allowed me to create a function and allow Python to try executing it. If the formula does not execute, I can leverage 'except' to instruct the application what to do (such as write the error to a log or display the error to the user). 'Finally' follows try and except and will execute whether or not the formula fails.
The script I created to test error handling is quite simple. It contains an IO class with two methods for getting data from the user (Figure 1). The user is asked to input two numbers that they would like to sum. It then advances to a processing class that contains the try and except formula. If the user attempts to add two items together that are integers, they will sum correctly and the result will be displayed. If the user attempts to add two items that are NOT integers, the following exception will be displayed "You did not enter a number for one of the variables!". (Figure 2)
Figure 1 |
Figure 2
The result looks like this:
|
Powered by TinyTake Screen Capture
Pickle Module
The pickle module allows us to convert data into a binary object and store it on a hard drive for retrieval at a later time. This is helpful to use in place of a database for data storage when a database is not necessary. the 'Dump' protocol (pickle.dump) allows for the data to be dumped into a binary file (figure 3).
Figure 3 |
The binary file now sits in the directory you specify waiting to be used later in the code. The load protocol (pickle.load) allows the pickle program to retrieve a binary file and load the data into the application as a list (Figure 4).
Figure 4 |
The most helpful introduction to the pickle module I could find was provided by JournalDev.com. JournalDev provided a simple example of of creating a list, dumping it to a binary file, and retreiving that file as a list. The example can be found here: https://www.journaldev.com/15638/python-pickle-example.
The result of my code is as follows:
Powered by TinyTake Screen Capture
Summary
Overall, error handling will be incredibly helpful to log and issues my programs my experience to better help me and others troubleshoot. The pickle module will come in handy when I'm working with large datasets and prefer not to store the data in memory- potentailly bogging down the application's performance.
Comments
Post a Comment