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.
Know a good pickle howto?
Collapse
X
-
Tags: None
-
-
It's really quite simple:Originally posted by spacecoyoteI 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.
Code:>>> import cPickle >>> strList = ['asf','dadf','adf','adf',] >>> pickleFile = open("pickleTest.dat", "w") >>> cPickle.dump(strList, pickleFile) >>> pickleFile.close() >>> pickleFile = open("pickleTest.dat") >>> unpickledList = cPickle.load(pickleFile) >>> print unpickledList ['asf', 'dadf', 'adf', 'adf'] >>> pickleFile.close() >>>Comment
-
-
We use Pickler and Unpickler in several of our applications to save and restore a dictionary or list of dictionaries.Originally posted by spacecoyoteI 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.
Library functions:Typical function calls:Code:import os import macrolib.pickle from param import Warning def import_data(file_name): try: f = open(file_name, "r") except IOError, e: # unable to open file Warning("Default file import error. Reverting to original defaults...") return None # file open is successful try: dd = macrolib.pickle.Unpickler(f).load() f.close() # test for a dictionary or list of dictionaries if isinstance(dd, dict): return dd elif isinstance(dd, list): for i, item in enumerate(dd): if not isinstance(item, dict): Warning("**INVALID** List item %s is not a dictionary." % (i)) return None # always return one dictionary ddr = {} for d in dd: ddr.update(d) return ddr else: # dd is not a dictionary or list of dictionaries Warning("Invalid imported data type.\nData must be a dictionary or list of dictionaries.") return None except: # file is not compatible with Unpickler - close file and warn user f.close() Warning("Invalid defaults file. Reverting to original defaults...") return None def export_data(file_name, dd): if check_Defaults_dir(os.path.dirname(file_name)): # directory exists or was created try: f = open(file_name, "w") macrolib.pickle.Pickler(f).dump(dd) f.close() return True except: Warning("Default values file export error.") return False else: # directory did not exist and the user chose 'no' to create directory Warning("The default values file export was aborted") return FalseModule pickle works with many data types. From Python Essential Reference:Code:## auto import defaults data if enabled if enable_default_import_export == "Enable": dd0 = import_data(os.path.join(default_file_path, def_file)) if dd0: for key, value in dd0.items(): exec "%s = %s" % (key, repr(value)) in None else: Warning("Invalid data - Reverting to original defaults") ## auto export default values to disk if enabled if enable_default_import_export == "Enable": export_data(os.path.join(default_file_path, def_file), dd_list)
"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 :-),
BVComment
-
I figured out how to do what I wanted to do (before you guys posted :p). But I'll have a look at these.Comment
Comment