pickling a dict

Dan Perl danperl at rogers.com
Wed Nov 3 01:58:10 CET 2004

First of all, your posting shows up as part of the "Newbie question" thread 
and not as a new thread.  You probably replied or followed up to a posting 
in that thread and you only changed the subject line.  Please don't do that.

Now, without your particular data, I personally cannot figure out why your 
script takes such a long time to load.  But I can say that your code and 
your use of pickle seem fine.  It shouldn't take minutes though, I have a 
script that loads a pickled file of about 15 Mb and it only takes seconds.

Are you sure that the bottleneck is in the pickle.load calls and not in your 
other functions (setVerticesDict, setEdgesDict, and setTrajectoryDict)? 
Have you profiled your script or did you try to insert something like a 
"print time.ctime(time.time())" statement in a few strategic places to find 
out whether pickle or the other functions are the bottleneck?

BTW, does your save( ) function also take minutes?  If it takes much less 
than the open( ) function, my guess would be that pickle is not the problem.


"andrea valle" <andrea.valle at unito.it> wrote in message 
news:mailman.5850.1099438033.5135.python-list at python.org...
I was saving on a file three dicts, for a total of ca. 950 keys (that
is: 24 kb).

In order to reopen the file,  first I read the three lines and used on
each eval(item).
This rude approach worked: but it took me some minutes to load the data.
So I used pickle (and also cPickle) supposing it should be much more
efficient. But it takes exactly the same.
Is it normal?

When I manipulate the loaded data ad draw the on screen the updated
result, everything is very fast.

  This are my two methods bound to a tk view:

     def save(self, event):
         # first approach
         ## self.file = file("/Graph/default.gra", "w")
         ## self.file.write(str(self.model.verticesDict)+"\n")
         ## self.file.write(str(self.model.edgesDict)+"\n")
         ## self.file.write(str(self.model.trajectoryDict)+"\n")
         ## self.file.close()
         self.file= file("/Graph/pickle.gra", "w")
         pickle.dump(self.model.verticesDict, self.file)
         pickle.dump(self.model.edgesDict, self.file)
         pickle.dump(self.model.trajectoryDict, self.file)

     def open(self, event):
         # first approach
         ## self.file = file("/Graph/default.gra", "r")
         ## verticesDict = eval(self.file.next())
         ## edgesDict = eval(self.file.next())
         ## trajectoryDict = eval(self.file.next())
         self.file = file("/Graph/pickle.gra", "r")
         verticesDict = pickle.load(self.file)
         edgesDict = pickle.load(self.file)
         trajectoryDict = pickle.load(self.file)

Am I using pickle in a bad way?
Or am I making some other mistake?



Andrea Valle
Laboratorio multimediale "G. Quazza"
Facoltà di Scienze della Formazione
Università degli Studi di Torino
andrea.valle at unito.it 

More information about the Python-list mailing list