Sujet : Re: How/where to store calibration values - written by program A, read by program B
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.pythonDate : 05. Dec 2023, 16:47:49
Autres entêtes
Organisation : Stefan Ram
Message-ID : <perm-20231205163734@ram.dialup.fu-berlin.de>
References : 1
Chris Green <
cl@isbd.net> writes:
Is there a neat, pythonic way to store values which are 'sometimes'
changed?
If you're comfortable with SQL, there is sqlite.
I'd write a small wrapper class around it, so that only this
wrapper comes in contact with any SQL. Then you can change
it later to store data in some other way (if necessary).
I could simply write the values to a file (or a database) and I
suspect that this may be the best answer but it does make retrieving
the values different from getting all other (nearly) constant values.
They are not /constant/ values.
When I was young and started to learn BASIC, one of the first
idea for my own programming language was the possibility to
define durations for variables. For example,
perm int i;
. Being declared "perm", your variable "i" now keeps its value
across program instances. So, when you say, "i = 27;", and do
not change it afterwars, and only one instance of your program
is running, next time you start your program, "i" will be 27.
A lazy way to save values would be to use one value per file.
(You do not need a special language like JSON, then.)
# WARNING: DO NOT RUN! (Program will override files named "x" and "y"!)
def load( filename ):
try:
with open( filename )as input_stream:
result = int( next( input_stream ))
except FileNotFoundError:
result = 0
return result
def save( value, filename ):
with open( filename, 'w' )as output_stream:
print( value, file=output_stream )
x = load( "x" )
y = load( "y" )
print( f'{x=}' )
print( f'{y=}' )
x += 1
y -= 1
save( x, "x" )
save( y, "y" )