By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

Know a good pickle howto?

P: 15
I need to know how to use pickle (and its brethren) correctly. Does anyone know of a good tutorial? The python manual is a bit too dense on this subject.
Jan 14 '07 #1
Share this Question
Share on Google+
8 Replies


Expert 100+
P: 511
how about this ?here
Jan 14 '07 #2

bartonc
Expert 5K+
P: 6,596
I need to know how to use pickle (and its brethren) correctly. Does anyone know of a good tutorial? The python manual is a bit too dense on this subject.
It's really quite simple:
Expand|Select|Wrap|Line Numbers
  1. >>> import cPickle
  2. >>> strList = ['asf','dadf','adf','adf',]
  3. >>> pickleFile = open("pickleTest.dat", "w")
  4. >>> cPickle.dump(strList, pickleFile)
  5. >>> pickleFile.close()
  6. >>> pickleFile = open("pickleTest.dat")
  7. >>> unpickledList = cPickle.load(pickleFile)
  8. >>> print unpickledList
  9. ['asf', 'dadf', 'adf', 'adf']
  10. >>> pickleFile.close()
  11. >>> 
Jan 14 '07 #3

bartonc
Expert 5K+
P: 6,596
how about this ?here
Great Post! I'll put that link into the new sticky (actually the parent page). Thanks gd
Jan 14 '07 #4

Expert 100+
P: 511
No prob :)
Jan 14 '07 #5

bartonc
Expert 5K+
P: 6,596
No prob :)
two posts to go...
Jan 14 '07 #6

bvdet
Expert Mod 2.5K+
P: 2,851
I need to know how to use pickle (and its brethren) correctly. Does anyone know of a good tutorial? The python manual is a bit too dense on this subject.
We use Pickler and Unpickler in several of our applications to save and restore a dictionary or list of dictionaries.
Library functions:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. import macrolib.pickle
  3. from param import Warning
  4.  
  5. def import_data(file_name):
  6.     try:
  7.         f = open(file_name, "r")
  8.     except IOError, e:
  9.         # unable to open file
  10.         Warning("Default file import error. Reverting to original defaults...")
  11.         return None
  12.  
  13.     # file open is successful
  14.     try:
  15.  
  16.         dd = macrolib.pickle.Unpickler(f).load()
  17.         f.close()
  18.         # test for a dictionary or list of dictionaries
  19.         if isinstance(dd, dict):
  20.             return dd
  21.         elif isinstance(dd, list):
  22.             for i, item in enumerate(dd):
  23.                 if not isinstance(item, dict):
  24.                     Warning("**INVALID** List item %s is not a dictionary." % (i))
  25.                     return None
  26.             # always return one dictionary
  27.             ddr = {}
  28.             for d in dd: ddr.update(d)
  29.             return ddr
  30.         else:
  31.             # dd is not a dictionary or list of dictionaries
  32.             Warning("Invalid imported data type.\nData must be a dictionary or list of dictionaries.")
  33.             return None
  34.  
  35.     except:
  36.         # file is not compatible with Unpickler - close file and warn user
  37.         f.close()
  38.         Warning("Invalid defaults file. Reverting to original defaults...")
  39.         return None
  40.  
  41. def export_data(file_name, dd):
  42.     if check_Defaults_dir(os.path.dirname(file_name)):
  43.         # directory exists or was created
  44.         try:
  45.             f = open(file_name, "w")
  46.             macrolib.pickle.Pickler(f).dump(dd)
  47.             f.close()
  48.             return True
  49.         except:
  50.             Warning("Default values file export error.")
  51.             return False
  52.  
  53.     else:
  54.         # directory did not exist and the user chose 'no' to create directory
  55.         Warning("The default values file export was aborted")
  56.         return False
Typical function calls:
Expand|Select|Wrap|Line Numbers
  1.     ## auto import defaults data if enabled
  2.     if enable_default_import_export == "Enable":
  3.         dd0 = import_data(os.path.join(default_file_path, def_file))
  4.         if dd0:
  5.             for key, value in dd0.items():
  6.                 exec "%s = %s" % (key, repr(value)) in None
  7.             else:
  8.                 Warning("Invalid data - Reverting to original defaults")
  9.     ## auto export default values to disk if enabled
  10.     if enable_default_import_export == "Enable":
  11.         export_data(os.path.join(default_file_path, def_file), dd_list)
Module pickle works with many data types. From Python Essential Reference:
"Multiple calls to the dump() and load() methods are allowed, provided that the sequence of load() calls used to restore a collection of previously stored objects matches the sequence of dump() calls during the pickling process."
HTH :-),
BV
Jan 14 '07 #7

P: 15
I figured out how to do what I wanted to do (before you guys posted :p). But I'll have a look at these.
Jan 15 '07 #8

bartonc
Expert 5K+
P: 6,596
I figured out how to do what I wanted to do (before you guys posted :p). But I'll have a look at these.
Did you notice that the docs say that cPickle can be 1000 times faster than the old pickle module?
Jan 15 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.