<$BlogRSDUrl$>

digitalfredy log

software libre. electronica, audio, video, ingenieria, ... en fin este es mi log dedicado a la tecnologia "que bacano que es ser cacharrero"

martes, mayo 23, 2017

Python: Exception Handling and Object Serialization 

This is my homework #7 for my UW Python course,

To handle exceptions in python we use try - except, in my case I'm using it to handle:

Pickle is used for Object Serialization, in our case loading/dumping a dictionary in a file. 

In additional I learn pprint to get a more clean dictionary printing. 

I tried to document all in my code then I'm posting it here:


#!/usr/bin/python

# Pickle: https://docs.python.org/2/library/pickle.html
# Pickle examples: https://wiki.python.org/moin/UsingPickle
import pickle
# Data pretty printer https://docs.python.org/2/library/pprint.html
import pprint

filename = "inventory.bin"

print "Assignment07 by Fredy Pulido-Lopez"
# Based on Assignement04 I did using try except 

print  "Importig and printing data from ",filename
# Exception handler to take care if the file is not present
try:
  with open (filename,'rb') as file:
      dictionary = pickle.load(file)
      pprint.pprint(dictionary)
except IOError:
  print "ERROR: Failed loading (IOError) ",filename,", starting from scratch"
  # No file loaded then i'm creating my dictionary here
  dictionary = []

print "############## Inventory composer ##############"

print "WARNING: Ctl+c will write ",filename," file."
# Exception handler, close and save when Ctl+c send a KeyboardInterrupt signal
try:
    # Infinite loop to feed the inventory in memory:
    while True:
        item = raw_input("Item   : "),
        value = raw_input("Value  : "),
        dictionary.append({'item': item, 'value': value})
except KeyboardInterrupt:
    print "\nWriting inventory in ",filename

# After the exception the loop is done and we have a feeded dictionary in memory
# Now we will save our dictionary in a file called inventory.txt 
with open (filename,'w') as file:
    pickle.dump(dictionary,file,-1)

posted by digitalfredy  # 12:23 a. m.

Archives

01/01/2004 - 02/01/2004   03/01/2004 - 04/01/2004   09/01/2004 - 10/01/2004   10/01/2004 - 11/01/2004   12/01/2004 - 01/01/2005   01/01/2006 - 02/01/2006   02/01/2006 - 03/01/2006   03/01/2006 - 04/01/2006   04/01/2006 - 05/01/2006   05/01/2006 - 06/01/2006   06/01/2006 - 07/01/2006   08/01/2006 - 09/01/2006   09/01/2008 - 10/01/2008   11/01/2008 - 12/01/2008   12/01/2008 - 01/01/2009   01/01/2009 - 02/01/2009   04/01/2009 - 05/01/2009   05/01/2009 - 06/01/2009   06/01/2009 - 07/01/2009   07/01/2009 - 08/01/2009   09/01/2009 - 10/01/2009   10/01/2009 - 11/01/2009   11/01/2009 - 12/01/2009   12/01/2009 - 01/01/2010   01/01/2010 - 02/01/2010   02/01/2010 - 03/01/2010   03/01/2010 - 04/01/2010   05/01/2010 - 06/01/2010   06/01/2010 - 07/01/2010   07/01/2010 - 08/01/2010   08/01/2010 - 09/01/2010   09/01/2010 - 10/01/2010   11/01/2010 - 12/01/2010   06/01/2011 - 07/01/2011   08/01/2011 - 09/01/2011   12/01/2011 - 01/01/2012   01/01/2012 - 02/01/2012   02/01/2013 - 03/01/2013   05/01/2017 - 06/01/2017  

This page is powered by Blogger. Isn't yours?