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


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

Re: Store a variable permanently

Started byJean-Michel Pichavant <jeanmichel@sequans.com>
First post2013-03-01 11:19 +0100
Last post2013-03-02 02:29 +0000
Articles 2 — 2 participants

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


Contents

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

#40244 — Re: Store a variable permanently

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2013-03-01 11:19 +0100
SubjectRe: Store a variable permanently
Message-ID<mailman.2716.1362133572.2939.python-list@python.org>
----- 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

funds=5
pickle.dump(funds, 'funds.pickle')

# to reload funds:

funds = pickle.load('funds.pickle')


The good thing with pickle is that it serializes a lot of things, see the "What can be pickled" section of the doc.


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.

[toc] | [next] | [standalone]


#40311

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-02 02:29 +0000
Message-ID<51316422$0$30001$c3e8da3$5496439d@news.astraweb.com>
In reply to#40244
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

[toc] | [prev] | [standalone]


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


csiph-web