Know a good pickle howto?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spacecoyote
    New Member
    • Nov 2006
    • 15

    Know a good pickle howto?

    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.
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    how about this ?here

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by spacecoyote
      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:
      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

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by ghostdog74
        how about this ?here
        Great Post! I'll put that link into the new sticky (actually the parent page). Thanks gd

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          No prob :)

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by ghostdog74
            No prob :)
            two posts to go...

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by spacecoyote
              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:
              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 False
              Typical function calls:
              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)
              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

              Comment

              • spacecoyote
                New Member
                • Nov 2006
                • 15

                #8
                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

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by spacecoyote
                  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?

                  Comment

                  Working...