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


Groups > comp.lang.python > #41058

Re: Store a variable permanently

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=7752596a8=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.035
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'newbie': 0.05; 'ascii': 0.07; 'dev': 0.07; "'w')": 0.09; 'tuple': 0.09; 'writable': 0.09; 'def': 0.10; "(i'm": 0.16; 'add.': 0.16; 'exited.': 0.16; 'integers,': 0.16; 'readable': 0.16; 'sources.': 0.16; 'subject:Store': 0.16; 'subject:variable': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.17; 'config': 0.17; 'variable': 0.20; 'holds': 0.20; 'import': 0.21; 'int,': 0.22; 'work.': 0.23; 'to:2**1': 0.23; 'raise': 0.24; 'header:In- Reply-To:1': 0.25; 'lines': 0.28; 'run': 0.28; '+0100,': 0.29; 'pickle': 0.29; 'steven': 0.29; 'url:mailman': 0.29; 'probably': 0.29; 'fri,': 0.30; 'code': 0.31; 'point': 0.31; 'url:python': 0.32; 'file': 0.32; '-----': 0.32; 'url:listinfo': 0.32; 'certain': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'data,': 0.35; 'skip:l 30': 0.35; 'add': 0.36; 'but': 0.36; 'url:org': 0.36; 'data.': 0.36; 'url:library': 0.36; 'anything': 0.36; 'should': 0.36; 'thank': 0.36; 'too': 0.36; 'uses': 0.37; 'being': 0.37; 'skip:v 20': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'you.': 0.61; 'received:194': 0.61; 'provide': 0.62; 'information': 0.63; 'more': 0.63; 'costs': 0.64; 'total': 0.65; 'importantly,': 0.65; 'person,': 0.65; 'dangerous': 0.66; 'disclose': 0.69; 'notice:': 0.71; 'privileged.': 0.72; 'funds': 0.78; '2013': 0.84; 'insecure': 0.84; 'needed:': 0.84; 'working,': 0.84; 'dozen': 0.91; 'medium.': 0.91; 'have.': 0.95
X-IronPort-AV E=Sophos;i="4.84,822,1355094000"; d="scan'208";a="1271290"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Mon, 11 Mar 2013 11:19:49 +0100 (CET)
From Jean-Michel Pichavant <jeanmichel@sequans.com>
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>, python-list@python.org
In-Reply-To <51316422$0$30001$c3e8da3$5496439d@news.astraweb.com>
Subject Re: Store a variable permanently
MIME-Version 1.0
X-Mailer Zimbra 7.2.2_GA_2852 (ZimbraWebClient - GC7 (Linux)/7.2.2_GA_2852)
Content-Type text/plain; charset="utf-8"
Content-Transfer-Encoding base64
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.3178.1362997259.2939.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362997259 news.xs4all.nl 6862 [2001:888:2000:d::a6]:39165
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41058

Show key headers only | View raw



----- Original Message -----
> On Fri, 01 Mar 2013 11:19:22 +0100, Jean-Michel Pichavant wrote:
> 
> > ----- Original Message -----
> >> So i have a variable called funds that i want to store the value
> >> of
> >> even after the program is exited. My funds variable holds the
> >> total
> >> value of funds i have. I add a certain number of funds each time i
> >> run
> >> the program by entering how much i want to add. How would i store
> >> the
> >> funds variable to keep its value? --
> >> http://mail.python.org/mailman/listinfo/python-list
> >> 
> >> 
> > Hi,
> > 
> > I would serialize the data.
> > 
> > http://docs.python.org/2/library/pickle.html
> 
> 
> I don't think we should recommend to a newbie that they use pickle
> without even warning them that using pickle is insecure and dangerous
> if
> they are opening pickles from untrusted sources.
> 
> But for a single int, pickle too is overkill, and a simple
> human-readable
> and writable file is probably all that is needed:
> 
> def save_value(n, configfile='myconfig'):
>     if n != int(n):
>         raise ValueError('expected an int')
>     with open(configfile, 'w') as f:
>         f.write("value=%d" % n)
> 
> def load_value(configfile='myconfig'):
>     with open(configfile) as f:
>         s = f.read().strip()
>     a, b = s.split("=", 1)
>     if a.strip() != "value":
>         raise ValueError('invalid config file')
>     return int(b)
> 
> 
> Untested but ought to work.
> 
> 
> --
> Steven

While your point about security is fair, the others aren't.
Pickle uses by default an ascii representation of the data, it's readable and writeable. 

import pickle
a = 758
pickle.dump(a, open('test.pickle', 'w'))
!cat test.pickle
I758
.

I don't see how 1 line of code (+ the import) can be overkill versus the dozen untested lines you provide (I'm sure it's working, my point being pickle has already been tested).
More importantly, if the code evolve and you need to store 2 integers, or a tuple or anything else that is pickable, it costs you 0 dev if you're using pickle.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

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


Thread

Re: Store a variable permanently Jean-Michel Pichavant <jeanmichel@sequans.com> - 2013-03-11 11:19 +0100
  Re: Store a variable permanently Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-12 10:17 +0000

csiph-web