Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41058 > unrolled thread
| Started by | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| First post | 2013-03-11 11:19 +0100 |
| Last post | 2013-03-12 10:17 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | Jean-Michel Pichavant <jeanmichel@sequans.com> |
|---|---|
| Date | 2013-03-11 11:19 +0100 |
| Subject | Re: Store a variable permanently |
| Message-ID | <mailman.3178.1362997259.2939.python-list@python.org> |
----- 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.
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-03-12 10:17 +0000 |
| Message-ID | <513f00cb$0$29965$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #41058 |
On Mon, 11 Mar 2013 11:19:49 +0100, Jean-Michel Pichavant wrote:
[...]
> 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
> .
What is that? It's not Python code, !cat test.pickle gives a syntax error.
By the way, you can dump pickles directly to a string, which may be more
convenient for demonstration purposes:
py> import pickle
py> pickle.dumps(758)
'I758\n.'
I take your point that a pickle of a simple int is relatively readable,
although it does require care when editing. If you drop the dot, or the
newline, or change the I to lowercase, or even merely add a space after
the dot, bad things happen.
But yes, I will concede that a single pickled int is relatively readable.
But that certainly isn't always the case:
py> pickle.dumps([])
'(lp0\n.'
py> pickle.dumps([None])
'(lp0\nNa.'
For even a *slightly* more complex example, the pickle turns into noise.
> 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).
Pickle is a big module, over 1400 lines, capable of serialising almost
anything. It's a big, powerful hammer for cracking armour-plated
coconuts. But a single int is pretty much a peanut. Compare pickle's 1400
lines with the dozen or so lines I provided. That is all that I meant by
"overkill".
> 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.
Sure. And once you move beyond a single value, the ability to call pickle
"human readable and writable" decreases rapidly. Without using pickle,
can you tell what this represents?
((dp0
S'y'
p1
I3
sS'x'
p2
I2
s(lp3
S'a'
p4
aS'b'
p5
aI23
tp6
.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web