Persistence

Geoff Gerrietts geoff at gerrietts.net
Thu Apr 11 17:14:22 EDT 2002


Quoting Rajarshi Guha (rxg218 at psu.edu):
> 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?

Shelve uses pickle under the hood -- you probably knew that but if you
didn't, now you do.

Shelve is effectively the equivalent of pickling a dictionary of
objects, one at a time, with the exception (I believe?) that you must
use strings as keys.

Shelve consequently allows you named access to your objects, and
allows you to store your objects in a disk cache, retrieving and
writing one at a time.

Pickle-to-a-file will dump a single object or multiple objects one
after the other. When un-pickling, they come back out one at a time. I
don't remember whether the pickle is a FILO or a FIFO, but it's EITHER
a queue or a stack.

That means no random access to your objects, but also no hash-table
overhead, and none of the limitations that go along with it (if you're
not using GDBM, your objects have a size limit, etc).

My preference for small graphs of objects is to dump the whole graph
and retrieve the whole graph and do my operations in-memory (for
definitions of small < 1MB). It's simpler, has fewer limitations, and
fewer dependencies. If I were in a situation where I were doing more
sophisticated things with larger volumes, I would consider shelve, but
probably also look very hard at ZODB or even just going to a full
relational DBMS.

Good luck,
--G.

-- 
Geoff Gerrietts             "That's it! I've had it with your sassy mouth!
<geoff at gerrietts net>     I didn't want to do this! (Well, actually, 
http://www.gerrietts.net/    I did....)"  -- Mojo Jojo, "Bubblevicious"





More information about the Python-list mailing list