[Tutor] Pickling
Suzanne Little
s349929@student.uq.edu.au
Mon, 26 Mar 2001 12:35:10 +1000 (GMT+1000)
Hello,
I'm trying to use pickle to store a DOM (from xml.dom.minidom). It appears
to perform the dump fine but I can't load the object. Is this because
minidom doesn't support pickling? Or am I mis-understanding something
about the way pickling works?
According to the Library Reference attempts to pickle an unpicklable
object will result in a PicklingError exception but I'm not getting this.
Sample session follows:
Python 2.0 (#1, Feb 14 2001, 10:51:28)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> from xml.dom.minidom import parse
>>> import pickle
>>> f = open('pickleTest', 'w')
>>> d = parse('xmlTest.xml')
>>> pickle.dump(d, f)
>>>
$ python2.0
Python 2.0 (#1, Feb 14 2001, 10:51:28)
[GCC 2.95.2 19991024 (release)] on sunos5
Type "copyright", "credits" or "license" for more information.
>>> import pickle
>>> f = open('xmlTest.xml', 'r')
>>> pickle.load(f)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 896, in
load
return Unpickler(file).load()
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 516, in
load
dispatch[key](self)
KeyError: <
>>>
Okay. Sometimes I get a blank line and nothing listed for the KeyError.
The xmlTest.xml file is listed below. I'm using python 2.0 on a solaris.
xmlTest.xml:
<?xml version="1.0"?>
<parent id="top"><child1 name="paul">Text goes here</child1>
<child2 name="fred">More text</child2>
</parent>
---
I have also had a few doms not pickle with the following error. There's a
lot more (it shoots up the screen) but here's the last bit. Notably the
xml that this dom was based on was a lot bigger and more complicated than
xmlTest
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 202, in
save
f(self, object)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 433, in
save_inst
save(stuff)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 202, in
save
f(self, object)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 382, in
save_dict
save(value)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 202, in
save
f(self, object)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 433, in
save_inst
save(stuff)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 202, in
save
f(self, object)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 381, in
save_dict
save(key)
File "/usr/local/stow/python-2.0/lib/python2.0/pickle.py", line 128, in
save
pid = self.persistent_id(object)
RuntimeError: Maximum recursion depth exceeded
>>>
Thanks as always,
Suzanne
BTW: Isn't pickle a great name! :) "What are you doing?" "I'm trying to
pickle my doms"
--------------------------------------------------------------------------
"Contrariwise," continued Tweedledee, "If it was so, it might be; and if
it were so, it would be; but as it isn't, it ain't. That's logic"
-Lewis Carroll
--------------------------------------------------------------------------