Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'handler': 0.04; 'unrecognized': 0.07; 'python': 0.08; 'absent': 0.09; 'correct.': 0.09; 'though:': 0.09; 'throw': 0.09; 'to:addr:comp.lang.python': 0.09; 'exception': 0.12; 'defaults.': 0.16; 'fruit': 0.16; 'ignored,': 0.16; 'subject: ..': 0.16; 'subject:handling': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'loading': 0.18; 'wrap': 0.18; 'cc:no real name:2**0': 0.21; 'header:In-Reply- To:1': 0.22; 'feb': 0.22; '(b)': 0.23; 'accidentally': 0.23; 'dictionary': 0.23; 'fine': 0.24; 'cache': 0.24; 'elements': 0.24; 'seconds': 0.26; 'cc:2**0': 0.26; 'code': 0.26; 'bit': 0.28; 'weird': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'sun,': 0.30; 'from:addr:googlemail.com': 0.31; 'header:User-Agent:1': 0.33; 'file': 0.34; 'anything': 0.34; '(a)': 0.34; 'keys': 0.34; 'safely': 0.34; 'something': 0.35; 'received:209.85.214': 0.36; 'two': 0.36; 'sequence': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'some': 0.38; 'think': 0.38; 'data': 0.38; 'sometimes': 0.38; 'received:209': 0.39; 'might': 0.40; "you'll": 0.61; 'more': 0.61; 'your': 0.61; 'course,': 0.62; 'zip': 0.62; 'chance': 0.62; 'love': 0.63; 'guarantee': 0.66; 'maximum': 0.66; 'crash': 0.67; '26,': 0.73; '8bit%:62': 0.77; '11:04': 0.84; '97%': 0.84; 'drinking': 0.84; 'otten': 0.84; 'skip:2 50': 0.91; 'absolutely': 0.98 Received-SPF: pass (google.com: domain of dihedral88888@googlemail.com designates 10.68.199.166 as permitted sender) client-ip=10.68.199.166; Authentication-Results: mr.google.com; spf=pass (google.com: domain of dihedral88888@googlemail.com designates 10.68.199.166 as permitted sender) smtp.mail=dihedral88888@googlemail.com Newsgroups: comp.lang.python Date: Mon, 27 Feb 2012 06:00:22 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=112.105.143.185; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw References: User-Agent: G2/1.0 X-Google-Web-Client: true MIME-Version: 1.0 Subject: Re: pickle handling multiple objects .. From: 88888 Dihedral To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330351225 news.xs4all.nl 6924 [2001:888:2000:d::a6]:36813 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20938 =E5=9C=A8 2012=E5=B9=B42=E6=9C=8826=E6=97=A5=E6=98=9F=E6=9C=9F=E6=97=A5UTC+= 8=E4=B8=8B=E5=8D=889=E6=97=B600=E5=88=8631=E7=A7=92=EF=BC=8CChris Angelico= =E5=86=99=E9=81=93=EF=BC=9A > 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 =3D pickle.load(f) > > > > you'll end up drinking potatoes. >=20 > You mean vodka? :) >=20 > 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. >=20 > 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: >=20 > data =3D pickle.load(f) > fruit =3D data.get("fruit",[]) > beverages =3D data.get("beverages",[]) > vegetables =3D data.get("vegetables",[]) >=20 > With this, you guarantee that (a) unrecognized keys will be safely > ignored, and (b) absent keys will quietly go to their given defaults. >=20 > ChrisA I love python for pickle and zip lib build in. I write programs that=20 stay in the L1 or L2 cache of the CPU > 97% percent of chance of nontrivial= executions.