pickling error after reload

15 views
Skip to first unread message

Ian

unread,
Oct 29, 2009, 5:55:03 PM10/29/09
to APAM Python Users
I'm using ipython, and after I use the "magic" reload to reload some
class definitions I get a pickling error. The error only happens
after I reload the file ("sai_classes") describing the classes (Phi
and Measurement) listed below. The error is:

PicklingError: Can't pickle <class 'sai_classes.Phi'>: it's not the
same object as sai_classes.Phi

However, I did not try to pickle a sai_classes.Phi object! The pickle
that caused this error was,
measurement.pickleme()
where measurement is a Measurement object, and measurement.pickleme is
inherited from SaiClasses.pickleme, which is defined as

def pickleme(self, name=None):
import cPickle

if name==None:
name_str = str(self.name) + '.pick'
else:
name_str = name + '.pick'
f = open(name_str,'w')
cPickle.dump(self, f)
f.close()

Ethan Coon

unread,
Oct 29, 2009, 8:23:51 PM10/29/09
to apam-pyt...@googlegroups.com
I've found pickling anything except the most basic of objects to be
extremely tricky and often is not worth the effort (i.e. custom
checkpointing where you re-initialize your objects from scratch and then
insert data from saved files is better).

I've also found that %reload screws up anything that plays with the MRO
(way python stores class inheritance information in new-style classes),
so it wouldn't surprise me if pickling after a reload is touchy. Since
I use mixins for a lot of my stuff, %reload screws everything up and
I've given up on it.


One place to look -- are new or old-style classes -- does
sai_classes.Phi inherit "object"? If not, it probably should.

Just a few things to look at, sorry I can't be more help. I can say
that pickling is much trickier than python makes it sound. I ended up
giving up on pickling in favor of having an "initialize" that can be
called anytime, and a "reinitialize" which initializes and then loads
all necessary data from disk for my checkpointing.

Ethan

--
-------------------------------------------
Ethan Coon
DOE CSGF - Graduate Student
Dept. Applied Physics & Applied Mathematics
Columbia University
212-854-0415

http://www.ldeo.columbia.edu/~ecoon/
-------------------------------------------

Reply all
Reply to author
Forward
0 new messages