[Tutor] additional persistence questions

dman dsh8290@rit.edu
Mon, 3 Dec 2001 19:52:21 -0500


On Mon, Dec 03, 2001 at 03:51:31PM -0500, fleet@teachout.org wrote:
| 
| 
| Your example below works; but when I try something like:
| 
| john = class('john doe','123-456-6789')
| jane = class('jane doe','123-456-9876')
| 
| I can pickle john, or I can pickle jane; but I don't seem to be able to
| pickle both.

Care to share your code?  (btw, that can't be your real code, 'class'
is a keyword ;-))

--- demo.py ---
import pickle
from cStringIO import StringIO

class ABookEntry :
    def __init__( self , name , phone ) :
        self.name = name
        self.phone = phone

    def __str__( self ) :
        return "%s %s" % ( self.name , self.phone )

john = ABookEntry( "john" , "123-4567" )
jane = ABookEntry( "jane" , "765-4321" )

print john , jane

f = StringIO() # this is a file-like object, but in memory only
pickle.dump( john , f )
pickle.dump( jane , f )

del john , jane

f.seek( 0 ) # go back to the beginning, as if it was just opened

jo = pickle.load( f )
ja = pickle.load( f )

print jo , ja
---------------

$ python demo.py 
john 123-4567 jane 765-4321
john 123-4567 jane 765-4321


Looking closer at the docs, the 'loads()' function only unpickles the
first object in the string and ignores the rest.


| I'm assuming to do this I need to store the data in a
| dictionary (or something) and pickle the dictionary (or something).
| 
| Actually, having pondered this through three screensaver activations (or
| so) it appears I would have to store the *instance* in a dictionary - not
| the name or phone number; but just "john."
| 
| >>> john
| <addrbook.AddrBookEntry instance at 80e62b0>

You don't need to create a dictionary for this, just dumping the
instances is sufficient.

| But would this be valid for different sessions of Python?

Dictionaries, lists, classes, class instances, strings, etc are all
in-memory data.  When you quit python (or turn off your computer) the
memory where those data were stored is released and used by other
processes.

What you are looking for is persistant storage of some kind.  You want
your data to persist beyond the lifetime of a given python process.
The only way to achieve this is to serialize the data to disk (or,
conceivably to memory on a different machine that will continue
running, but that is not so feasible).  To serialize the data do disk
means either creating files or using a full-blown database system that
will handle files for you.

The simplest thing for you is to create a file yourself and to use the
'pickle' module to convert the in-memory data to a string that can be
stored in the file, and (very important!) read back in later.  See the
example above for how to do this.  However, don't use the StringIO or
cStringIO modules -- they provide file-like objects that exist in
memory.  They go away when you quit python.  I used them just so I
didn't need to create files on my disk for the example.

-D

-- 

Contrary to popular belief, Unix is user friendly.
It just happens to be selective about who it makes friends with.
                                               -- Dave Parnas