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


Groups > comp.lang.python > #20890 > unrolled thread

Re: pickle handling multiple objects ..

Started byPeter Otten <__peter__@web.de>
First post2012-02-26 13:04 +0100
Last post2012-02-26 13:04 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#20890 — Re: pickle handling multiple objects ..

FromPeter Otten <__peter__@web.de>
Date2012-02-26 13:04 +0100
SubjectRe: pickle handling multiple objects ..
Message-ID<mailman.176.1330257807.3037.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web