Persistence

Quinn Dunkan quinn at lira.ugcs.caltech.edu
Sun Jun 16 15:12:25 EDT 2002


On Thu, 11 Apr 2002 16:10:17 -0400, Rajarshi Guha <rxg218 at psu.edu> wrote:
>On Thursday 11 April 2002 13:58 in comp.lang.python Riccardo de Maria wrote:
>
>> Is there a way to save in a file the status, objects, functions of
>> interpreter
>> in order to restore it after the interpreter has been closed?
>> 
>> If it is possible, one can work on a project stop and then continue
>> later adding functions, objects and so on.
>
>I have a related question - does the OP refer to object persistence or 
>object serialization? Is there a difference between the two?
>
>I looked at the shelve and pickle modules - the former uses a dbm database 
>whereas the latter just dumps an ASCII representation of the object. The 
>fact that shelve uses a dbm indicates that it should be more efficient. Is 
>this true? When would I choose one over the other?

That's only true if you leave bin=0 when you call dump().  For best efficiency,
use cPickle with bin=1.

A quick glance at shelve.py reveals that it uses pickle, so it can hardly
be more efficient.

The difference is that pickle takes a single object and serializes it to a
string, which can then be written to a file.  shelve does the same thing only
it writes it to a dbm, so you could have many pickles and retrieve them by
name.

If you don't have lots of pickles or if you're using ReiserFS, you could get
the dbm effect by just sticking a bunch of pickles in a directory.  Lately,
I've been "pickling" by just doing 'f=open(obj_name); pprint.pprint(obj, f)'
and eval(open(obj_name).read()), which has the advantage of giving easy-to-
read, easy-to-edit files.  Works nicely if your data is simple.

> On Thu, 11 Apr 2002 16:10:17 -0400, Rajarshi Guha <rxg218 at psu.edu> wrote:

Of course, everything said is with the utmost respect.  As ever, I am y'r
humblest servant.  If you please, and if it is not any trouble, I would
appreciate a kind word to some of the higher-ups.  If there are any.

Operation Rear-view Mirror has been completed and I await further instructions.



More information about the Python-list mailing list