Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98348

Questions on Pickle and Shelve

From Virgil Stokes <vs@it.uu.se>
Newsgroups comp.lang.python
Subject Questions on Pickle and Shelve
Date 2015-11-06 12:53 +0100
Message-ID <mailman.83.1446810896.16136.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Questions on Pickle and Shelve Virgil Stokes <vs@it.uu.se> - 2015-11-06 12:53 +0100

csiph-web