July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Luther Blissett
You want to
save and restore class and instance objects using the
cPickle module.
Often, you need no special precautions to use
cPickle on your classes and their instances. For
example, the following works fine:
import cPickle
class ForExample:
def _ _init_ _(self, *stuff): self.stuff = stuff
anInstance = ForExample('one', 2, 3)
saved = cPickle.dumps(anInstance)
reloaded = cPickle.loads(saved)
assert anInstance.stuff == reloaded.stuffHowever, sometimes there are problems:
anotherInstance = ForExample(1, 2, open('three', 'w'))
wontWork = cPickle.dumps(anotherInstance)This causes a TypeError: “can’t
pickle file objects exception”, because the state of
anotherInstance includes a file object, and file
objects cannot be pickled. You would get exactly the same exception
if you tried to pickle any other container that includes a file
object among its items.
However, in some cases, you may be able to do something about it:
import types
class PrettyClever:
def _ _init_ _(self, *stuff): self.stuff = stuff
def _ _getstate_ _(self):
def normalize(x):
if type(x) == types.FileType:
return 1, (x.name, x.mode, x.tell( ))
return 0, x
return [ normalize(x) for x in self.stuff ]
def _ _setstate_ _(self, stuff):
def reconstruct(x):
if x[0] == 0:
return x[1]
name, mode, offs = x[1]
openfile = open(name, mode)
openfile.seek(offs)
return openfile
self.stuff = tuple([reconstruct(x) for x in stuff])By defining the _ _getstate_ ...
Read now
Unlock full access