Pickling objects...

Matt Russell matt at teaphoo.fsnet.co.uk
Fri Apr 25 18:42:24 EDT 2003


Hi,

you should not override the class's namespace.
i.e do not define class docContainer twice.

In the ocde you posted , the script that sets state defines 'class
docContainer',
and in the code that reads it you've overriden the class to be a blank
class of the same name.
if you need that class in the script that reads state, simply import
it from the script that sets state.

i.e

from script1 import docContainer

packedObj = open('packed.file', 'r')
doc = cPickle.load(packedObj)

Cheers,
Matt

"Bo M. Maryniuck" <b.maryniuk at forbis.lt> wrote in message news:<mailman.1051291900.11523.python-list at python.org>...
> Hello.
> 
> Well, I would like to anybody help me to serialize an instance 
> from 4DOM HtmlLib. Somebody know where (and HOW) to push 
> __setstate__ / __getstate__ methods? 8-| I tried a lot of different
> code, but _always_ I got the same traceback (below).
> 
> Yes, I know, I need __setstate__ stuff, but here I dumb where to
> push these... Or it is impossible to serialize it?.. Simple classes serialises
> without the problem. But this.......
> 
> This pack's OK:
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
> #!/usr/bin/python
> from xml.dom.ext.reader import HtmlLib as HL
> import cPickle
> 
> class docContainer:
>     def __init__(self, doc):
>         self.doc = doc
> 
>     def __getstate__(self):
>         return self.__dict__.copy()
> 
>     def __setstate__(self, dict):
>         self.__dict__.update(dict)
> 
> reader = HL.Reader()
> doc = docContainer(reader.fromString(open('foo.html').read(), None, None))
> 
> packIt = open('packed', 'w')
> p = cPickle.Pickler(packIt, 1)
> p.dump(doc)
> packIt.close()
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
> 
> This should read, but raises an exception (below):
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
> #!/usr/bin/python
> import cPickle
> 
> class docContainer:
>     pass
> 
> packedObj = open('packed.file', 'r')
> doc = cPickle.load(packedObj)
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
> 
> ...and the traceback:
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
> Traceback (most recent call last):
>   File "./unpkl.py", line 10, in ?
>     doc = cPickle.load(packedObj)
> TypeError: ('__init__() takes exactly 1 argument (2 given)', <class xml.dom.html.HTMLDocument.HTMLDocument at 0x81dd47c>, (None,))
> * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
> 
> Can anybody push me where to find and WHAT to subclass, if I need it...?
> Thank you.




More information about the Python-list mailing list