Questions on Pickle and Shelve

Virgil Stokes vs at it.uu.se
Fri Nov 6 06:53:43 EST 2015


Here is snippet of Python (vers. 2.7.10) code that bothers me.

import cPickle as pickle

print "Pickle lists:"
dogs = ['Rover','King','Spot','Rufus']
cats = ['Mimi','Misty','Sasha']

with open('pickle.dat', 'wb') as pfile:
     pickle.dump(dogs, pfile)
     pickle.dump(cats,pfile)

del(dogs); del(cats)
with open('pickle.dat', 'rb') as pfile:
     dogs = pickle.load(pfile)
     cats = pickle.load(pfile)
     print dogs, '\n', cats, '\n'

import shelve

# Note! __exit__ attribute undefined for shelve
sfile = shelve.open('shelve.dat')
sfile['dogs'] = dogs
sfile['cats'] = cats
sfile.close()

print "Shelve entries:"
del(cats); del(dogs)
sfile = shelve.open('shelve.dat')
#print sfile
for key in sfile.keys():
     print key,' - ',sfile[key]
sfile.close()

1)  Which (the pickle or shelve code) takes less total RAM, if dogs and cats 
were very large?
2)  When the last shelve.open is given, is the entire contents of shelve.data 
transferred to RAM?  Note, if the print sfile is uncommented then the entire 
contents of shelve.data is printed out.

I was under the impression that the entire contents of a shelved file was not 
transferred to RAM when it was opened.





More information about the Python-list mailing list