Pickling objects...

Bo M. Maryniuck b.maryniuk at forbis.lt
Fri Apr 25 14:31:42 EDT 2003


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.

-- 
Regards, Bogdan

Only great masters of style can succeed in being obtuse.
                -- Oscar Wilde






More information about the Python-list mailing list