pickling a dict

andrea valle andrea.valle at unito.it
Tue Nov 2 18:27:05 EST 2004


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)
         self.file.close()

     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)
         self.model.setVerticesDict(verticesDict)
         self.model.setEdgesDict(edgesDict)
         self.model.setTrajectoryDict(trajectoryDict) 

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

Thanks

-a-



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