[Tutor] Saving class to a file!!

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon Dec 2 02:53:21 2002


On Thu, 28 Nov 2002, Tiago Duarte Felix wrote:

> well.. class.. was just a name... i used a variable of course.... Code
> is really too long to be pasted... plus... variable names and comments
> are in portuguese..... but anyway.. i took some parts of it.... here it
> goes: if anyoe can tell me what i am doing wrong.. please.... thanks!!!!

Hi Tiago,

Just doing a followup: Have you been able to resolve the problem yet with
the pickling code?



> cPickle.dump((prj,conf,sim,fichs),open(prj.path+prj.n_fich,'wb'),true)

This line might be problematic.  When we open files for writing, it's
often a good idea to use an explicit name for them, so that we can call
close()  when we're done writing to the file.  This may seem a little
silly, but there may be caching behavior that may not be completely
writing the pickled stream to disk.

(I'm suspicious about it particularly because the code is writing things
as a binary stream, so buffering won't be line based, but block-based.)


You may want to try:

###
myfile = open(prj.path+prj.n_fich,'wb')
cPickle.dump((prj,conf,sim,fichs), myfile, true)
myfile.close()
###

or, better yet, make sure that the binary stuff isn't the issue, and use
the native 'pickle' module.  We're not so concerned with efficiency as we
are with correctness at the moment: let's make sure that it's not the
pickling process itself that's at fault:

###
import pickle
myfile = open(prj.path+prj.n_fich, 'w')
pickle.dump((prj,conf,sim,fichs), myfile, true)
myfile.close()
###


(In your last message, you mentioned that the system died with an
operating system error.  That's not good at all!

My best guess so far is that cPickle, the C extension module, didn't do
the right thing at all when given an incorrect pickle!  cPickle's written
in C for speed, so I wouldn't be too surprised if there were some
unbounded array bugs in that code.  You may want to send a bug report to
Sourceforge, including a copy of the pickle file as well as the class
definitions, so that the developers can take a close look.)




By the way, your code is trying to save those four objects by dumping them
into a single tuple at once.  It might be more convenient to use the
'shelve' module, which uses 'pickle' underneath the surface, but looks
much like a dictionary.

    http://www.python.org/doc/lib/module-shelve.html


So your code might look more like:

###
d = shelve.open(prj.path+prj.n_fich)
d['prj'] = prj
d['conf'] = conf
d['sim'] = sim
d['fichs'] = fichs
d.close()
###

which makes it more convenient to add additional objects to be serialized,
without having to worry as much about the order of the elements in a
tuple.



I hope that this helps!