rfmonk

pickle_dump_to_file_1.py

Jan 29th, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #1/usr/bin/env python
  2.  
  3. # this is from The Python
  4. # Standard Library by example
  5. # ISBN13: 9780321767349
  6.  
  7. try:
  8.     import cPickle as pickle
  9. except:
  10.     import pickle
  11. import sys
  12.  
  13.  
  14. class SimpleObject(object):
  15.     def __init__(self, name):
  16.         self.name = name
  17.         l = list(name)
  18.         l.reverse()
  19.         self.name_backwards = ''.join(l)
  20.         return
  21.  
  22. if __name__ == '__main__':
  23.     data = []
  24.     data.append(SimpleObject('pickle'))
  25.     data.append(SimpleObject('cPickle'))
  26.     data.append(SimpleObject('last'))
  27.  
  28.     filename = sys.argv[1]
  29.  
  30.     with open(filename, 'wb') as out_s:
  31.         # Write to the stream
  32.         for o in data:
  33.             print 'WRITING: %s (%s)' % (o.name, o.name_backwards)
  34.             pickle.dump(o, out_s)
Add Comment
Please, Sign In to add comment