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


Groups > comp.lang.python > #20894

Re: pickle handling multiple objects ..

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'handler': 0.04; 'unrecognized': 0.07; 'absent': 0.09; 'correct.': 0.09; 'though:': 0.09; 'throw': 0.09; 'exception': 0.12; 'defaults.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'fruit': 0.16; 'ignored,': 0.16; 'subject: ..': 0.16; 'subject:handling': 0.16; 'received:74.125.82.44': 0.16; 'received :mail-ww0-f44.google.com': 0.16; 'wrote:': 0.18; 'loading': 0.18; 'wrap': 0.18; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; '(b)': 0.23; 'accidentally': 0.23; 'dictionary': 0.23; 'fine': 0.24; 'elements': 0.24; 'seconds': 0.26; 'code': 0.26; 'bit': 0.28; 'message-id:@mail.gmail.com': 0.29; 'weird': 0.29; 'pm,': 0.29; 'sun,': 0.30; 'file': 0.34; 'anything': 0.34; '(a)': 0.34; 'keys': 0.34; 'safely': 0.34; 'to:addr:python-list': 0.35; 'something': 0.35; 'received:74.125.82': 0.36; 'two': 0.36; 'sequence': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'some': 0.38; 'think': 0.38; 'received:74.125': 0.38; 'data': 0.38; 'sometimes': 0.38; 'might': 0.40; 'to:addr:python.org': 0.40; "you'll": 0.61; 'header:Received:6': 0.61; 'more': 0.61; 'your': 0.61; 'course,': 0.62; 'guarantee': 0.66; 'maximum': 0.66; 'crash': 0.67; '26,': 0.73; '11:04': 0.84; 'drinking': 0.84; 'otten': 0.84; 'received:10.180': 0.84; 'absolutely': 0.98
Received-SPF pass (google.com: domain of rosuav@gmail.com designates 10.180.95.34 as permitted sender) client-ip=10.180.95.34;
Authentication-Results mr.google.com; spf=pass (google.com: domain of rosuav@gmail.com designates 10.180.95.34 as permitted sender) smtp.mail=rosuav@gmail.com; dkim=pass header.i=rosuav@gmail.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=8hLM7gAQJakthXUHpB1Xm+tE4NUd/IsSZwLJHazhvTs=; b=ptaesC53G64nvRd90UrkTxW6VKubuYkogMOlfRAIWWuMR3lXbuRl0ISMFgXnxCLIkU GDYLf6z4EeZoWuSYuY5IM9ju0V57WEST6YcOc0Laz4ABT9/vs+y9jjg7QvfGD/wZG6SN h3BFPjXLGw0XJDCvEQbbquX1p7IkFQjkfq1JI=
MIME-Version 1.0
In-Reply-To <jid71t$eh6$1@dough.gmane.org>
References <CAJ1erZ0s-aEzD_uYqY4=bhbwAkBRykXD72Lfx7bCmecRDazhAw@mail.gmail.com> <jid71t$eh6$1@dough.gmane.org>
Date Mon, 27 Feb 2012 00:00:31 +1100
Subject Re: pickle handling multiple objects ..
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.178.1330261239.3037.python-list@python.org> (permalink)
Lines 34
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330261239 news.xs4all.nl 6863 [2001:888:2000:d::a6]:50563
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20894

Show key headers only | View raw


On Sun, Feb 26, 2012 at 11:04 PM, Peter Otten <__peter__@web.de> wrote:
> 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.

You mean vodka? :)

Additionally, you'll get a weird crash out of your program if load()
returns something other than a sequence of length 3. Remember,
everything that comes from outside your code is untrusted, even if you
think you made it just two seconds ago.

Of course, sometimes that exception is absolutely correct. If you wrap
all this in an exception handler that gives some reasonable behaviour
- which might even be "terminate the program with a traceback", which
is the default - then it's fine to let it throw on failure, and
anything else is just a waste of effort. But for maximum
extensibility, you would want to make it so that you can add more
elements to what you save without your code breaking on an old save
file - and that's where the dictionary is far better. A slight tweak,
though:

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

With this, you guarantee that (a) unrecognized keys will be safely
ignored, and (b) absent keys will quietly go to their given defaults.

ChrisA

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


Thread

Re: pickle handling multiple objects .. Chris Angelico <rosuav@gmail.com> - 2012-02-27 00:00 +1100
  Re: pickle handling multiple objects .. 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-27 06:00 -0800
  Re: pickle handling multiple objects .. 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-27 06:00 -0800

csiph-web