Object Persistence and Pickle&zlib

thomas at bibsyst.no thomas at bibsyst.no
Fri Nov 12 10:01:22 CET 1999


Hi,

I got a object declared like this ( this is an example, the actual code
is much bigger ) :

class some_object:
    title = ''
    id = 0
    items1 = {}
    items2 = {}
    items3 = {}
    items4 = {}

    def some_method1(self):
        ...

    def some_method2(self):
        ...

    def some_method3(self):
        ...

    def some_method4(self):
        ...

which I create like this :

x = some_object()
x.id = 2
x.title = 'test'

and use like

x.some_method1() # which fills up the dictionaries in the object with
various data
x.some_method2() # the same
x.some_method3() # lists out the data in an object for instance

and I store the object like

import shelve
db = shelve.open('objects')
db[str(x.id)] = x
db.close()

but when I get the object later, some methods and data seem to be
missing.
For example, the first three dictionaries in the object might have the
data I stored in them, but the last one is empty. Some of the methods
are available, but some are missing. Eh ... what is going on?

Do I have to initialize vars in any way to make them stick? The state of
the object is the same each time I fetch it from a datafile.

I also want to use compression on these objects, since the seem to be
reduced 1/10 in size. I do something like :

data = pickle.dump(x)
data = zlib.compress(data)

then store it like so :

db[str(x.id)] = data

When I try to fetch the object I get an -5 error from zlib :

data = zlib.decompress(db[id])

What does that error mean?

Thanks for helping a python-newbie out.

Thomas Weholt






More information about the Python-list mailing list