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


Groups > comp.lang.python > #20890

Re: pickle handling multiple objects ..

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'happily': 0.07; 'python': 0.08; '-------': 0.09; 'executed': 0.09; 'linux.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'output': 0.10; "'orange',": 0.16; "'rb')": 0.16; '4321': 0.16; 'fruit': 0.16; 'objects?': 0.16; 'output)': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject: ..': 0.16; 'subject:handling': 0.16; 'unpack': 0.16; 'wrote:': 0.18; 'loading': 0.18; 'accidentally': 0.23; 'from:addr:web.de': 0.23; 'ignore': 0.24; 'fine': 0.24; "python's": 0.24; 'code': 0.26; 'import': 0.27; '-----': 0.28; 'bit': 0.28; 'example': 0.29; 'print': 0.29; 'skip:b 20': 0.29; 'url:library': 0.31; 'objects': 0.32; 'it.': 0.33; 'object': 0.33; 'header:X-Complaints-To:1': 0.34; 'skip:# 10': 0.34; 'keys': 0.34; 'probably': 0.35; 'to:addr:python-list': 0.35; 'something': 0.35; 'url:python': 0.35; 'received:org': 0.36; 'instead,': 0.37; 'but': 0.37; 'using': 0.37; 'somewhat': 0.38; 'should': 0.38; 'data': 0.38; 'easier': 0.38; 'skip:o 20': 0.38; 'url:org': 0.39; 'to:addr:python.org': 0.40; "you'll": 0.61; 'follow': 0.61; 'more': 0.61; 'your': 0.61; 'below': 0.62; 'harder': 0.64; 'drinking': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: pickle handling multiple objects ..
Date Sun, 26 Feb 2012 13:04 +0100
Organization None
References <CAJ1erZ0s-aEzD_uYqY4=bhbwAkBRykXD72Lfx7bCmecRDazhAw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50848fd3.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.176.1330257807.3037.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330257807 news.xs4all.nl 6970 [2001:888:2000:d::a6]:45358
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20890

Show key headers only | View raw


Smiley 4321 wrote:

> If I have a sample python code to be executed on Linux. How should  I
> handle multiple objects with 'pickle' as below -
> 
> -------
> #!/usr/bin/python
> 
> import pickle
> 
> #my_list = {'a': 'Apple', 'b': 'Mango', 'c': 'Orange', 'd': 'Pineapple'}
> #my_list = ('Apple', 'Mango', 'Orange', 'Pineapple')
> my_list = ['Apple', 'Mango', 'Orange', 'Pineapple']
> #my_list = ()
> output = open('readfile.pkl', 'wb')
> pickle.dump(my_list, output)
> output.close()
> 
> my_file = open('readfile.pkl', 'rb')
> my_list2 = pickle.load(my_file)
> my_file.close()
> 
> print my_list
> print my_list2
> -----
> 
> This code works fine but now I have to handle multiple objects?

You never do that with pickle. You pack your data into a single object 
before you store it, and unpack it after loading it. Here's an example using 
a tuple as the container:

fruit = ["apple", "orange"]
vegetables = ["potato", "tomato"]
beverages = ["milk", "coffee", "water"]

with open("tmp.pickle", "wb") as f:
    pickle.dump((fruit, vegetables, beverages), f)

with open("tmp.pickle", "rb") as f:
    fruit, vegetables, beverages = pickle.load(f)

print fruit
print vegetables
print beverages

This is however a bit errorprone. If you accidentally write the loading code 
as 

fruit, beverages, vegetables = pickle.load(f)

you'll end up drinking potatoes. A better container would be a dict. 
Something like

pickle.dump(dict(fruit=fruit, vegetables=vegetables, beverages=beverages), 
f)
...
data = pickle.load(f)
fruit = data["fruit"]
beverages = data["beverages"]
vegetables = data["vegetables"]

is harder to get wrong. It is also easier to extend. Code that only reads 
the pickle will happily ignore extra keys in the dictionary. If you follow 
that path somewhat more you will probably drop the lowlevel pickle and use a 
key-value store like Python's shelve instead, see

http://docs.python.org/library/shelve.html

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


Thread

Re: pickle handling multiple objects .. Peter Otten <__peter__@web.de> - 2012-02-26 13:04 +0100

csiph-web