473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Difference between Pickle and Shelve?

Python's docs say that Shelve uses Pickle to serialize its data.
However, I've noticed that Pickle can maintain internal links, while
Shelve cannot. For instance:
d = shelve.open('shelvedata.txt',writeback=True)
d['a']=[1]
d['b']=d['a']
print d {'a': [1], 'b': [1]}
d['a'][0]=2
print d {'a': [2], 'b': [2]}
d.close()
d = shelve.open('shelvedata.txt',writeback=True)
print d {'a': [2], 'b': [2]}
d['a'][0]=3
print d {'a': [3], 'b': [2]} <- BAD d={}
d['a']=[1]
d['b']=d['a']
d {'a': [1], 'b': [1]} d['a'][0]=2
d {'a': [2], 'b': [2]} outfile=open('pickledata.txt','w')
import pickle
pickle.dump(d,outfile)
outfile.close()
infile=open('pickledata.txt','r')
d=pickle.load(infile)
infile.close()
d {'a': [2], 'b': [2]} d['a'][0]=3
d

{'a': [3], 'b': [3]} <- GOOD

Pickle is a bit more work, but it's able to maintain these internal
links AND generates a considerable smaller file (44bytes with Pickle
compared to 24Kbytes with Shelve). Why does Shelve create such huge
files? However, since Shelve is slightly more streamlined, is there
any way to get the same functionality out of it as Pickle? If Shelve
uses Pickle why is there any discontinuity?
Jul 18 '05 #1
1 6703
go****@hanger.snowbird.net (Kris Caselden) writes:
Python's docs say that Shelve uses Pickle to serialize its data.
However, I've noticed that Pickle can maintain internal links, while
Shelve cannot.
shelve also maintains internal links - just not links across different
keys.
Pickle is a bit more work, but it's able to maintain these internal
links AND generates a considerable smaller file (44bytes with Pickle
compared to 24Kbytes with Shelve). Why does Shelve create such huge
files?


shelve is a database system capable of storing multiple objects in a
single file. Each individual object is stored with pickle.

So if you want to store only a single (root) object; if you want to
independently load and store independent objects, use shelve.

Regards,
Martin
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Adrian B. | last post by:
Newbie question; does shelve use cPickle or Pickle? Can it be specified? thanks
8
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the...
2
by: Johan Kohler | last post by:
Hi, I have a class with attributes that are string, integer and list. eg. class person: name ="" age = 0 friends= comment=""""""
4
by: temposs | last post by:
I'm trying to pickle a class, and while I get no errors or anything, almost none of the class instance gets pickled, and I don't know why...Here's the pickled output: (i__main__ TrainingMatrix...
2
by: Math | last post by:
Hello, I wonder if someone can help me out. My native is Dutch, sorry for this.... But I am writing this program, a program which uses Relational DataBase for saving all kinds of information...
6
by: amaltasb | last post by:
Can I use Pickle to store about 500,000 key value pairs.. or should I use mySql. Which one is best for performance, as the key value pair increases. Thanks
10
by: crystalattice | last post by:
I'm creating an RPG for experience and practice. I've finished a character creation module and I'm trying to figure out how to get the file I/O to work. I've read through the python newsgroup...
0
by: Guillaume Bog | last post by:
Hello, I read and re-read "Python in a Nutshell" written by Alex Martelli, who knows what he is talking about. I'm a bit new to python and I'm going to start doing persistence side on a project....
1
by: Gabriel Genellina | last post by:
En Sun, 18 May 2008 00:14:19 -0300, Guillaume Bog <guibog@gmail.comescribió: A shelve is just a persistent dictionary that uses pickle to store the objects. If you want to store one or a few...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.