In A Pickle Over Pickle()

Rod. rpalmer at connect.com.au
Thu Jan 20 19:58:01 EST 2000


its pretty easy, one way of doing it is to create a 'blank'
holder class that you fill up with all the state you want preserved.

ie:

>>> import pickle
>>> class foo: pass
>>> f = foo()
>>> f.title = 'test pickle'
>>> f.somedata = { 'a': 1 }
>>> f.flag = 0
>>> fd = open('foo.pickle','w')
>>> pickle.dump(f,fd)
>>> fd.close()

then 

>>> import pickle
>>> class foo: pass
>>> fd2 = open('foo.pickle','r')
>>> z = pickle.load(fd2)
>>> z.title
   'test pickle'

hope this makes it clear. 

Rod.


: I'm using Python to create a text-adventure writing system, and need
: to use pickle to save/restore the program state. The problem is, I
: seem to have a mental block when it comes to Pickle--I've read all the
: documentation I can find on pickle() and can't make heads or tails of
: it. I suspect I'm missing some obvious "first clue" that would make it
: come together.
: 
: Does anyone know where there's a good pickle() tutorial? Or, better
: still, has somebody already written a module to do what I need?
: 
: The program structure is very simple, 3 library modules imported by a
: "skeleton" that has no variables of its own. Basically what I want to
: do is pickle/unpickle the *entire* mess, variables, dictionaries, etc,
: of all three modules, and store them in a single file, preferably
: calling wrapper code that resides in one of the 3 libraries.
: 
: Any help appreciated!
: 
: Respectfully,
: 
: Wolf
: 
: "The world is my home, it's just that some rooms are draftier than
: others". -- Wolf
: -- 
: http://www.python.org/mailman/listinfo/python-list
: 

Rod Palmer +61 3 9251 3685 : bwarff at connect.com.au




More information about the Python-list mailing list