Is there a neat, pythonic way to store values which are 'sometimes'
changed?
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.
Is there a neat, pythonic way to store values which are 'sometimes'
changed?
My particular case at the moment is calibration values for ADC inputs
which are set by running a calibration program and used by lots of
programs which display the values or do calculations with them.
From the program readability point of view it would be good to have a
Python module with the values in it but using a Python program to write/update a Python module sounds a bit odd somehow.
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.
Are there any Python modules aimed specifically at this sort of
requirement?
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.
On 5 Dec 2023, at 14:37, Chris Green via Python-list <python-list@python.org> wrote:
Are there any Python modules aimed specifically at this sort of
requirement?
Is there a neat, pythonic way to store values which are 'sometimes'
changed?
My particular case at the moment is calibration values for ADC inputs
which are set by running a calibration program and used by lots of
programs which display the values or do calculations with them.
From the program readability point of view it would be good to have a
Python module with the values in it but using a Python program to write/update a Python module sounds a bit odd somehow.
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.
Are there any Python modules aimed specifically at this sort of
requirement?
Is there a neat, pythonic way to store values which are 'sometimes'
changed?
My particular case at the moment is calibration values for ADC inputs
which are set by running a calibration program and used by lots of
programs which display the values or do calculations with them.
From the program readability point of view it would be good to have a
Python module with the values in it but using a Python program to write/update a Python module sounds a bit odd somehow.
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.
Are there any Python modules aimed specifically at this sort of
requirement?
On 2023-12-05 14:37, Chris Green via Python-list wrote:
Is there a neat, pythonic way to store values which are 'sometimes'Some kind of key/value store sounds like the correct solution. I
changed?
My particular case at the moment is calibration values for ADC inputs
which are set by running a calibration program and used by lots of
programs which display the values or do calculations with them.
From the program readability point of view it would be good to have a
Python module with the values in it but using a Python program to
write/update a Python module sounds a bit odd somehow.
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.
Are there any Python modules aimed specifically at this sort of
requirement?
wouldn't go as far a database - that's overkill for a few calibration
values.
I might suggest TOML, except that Python's tomllib (Python 3.11+) is read-only!
Personally, I'd go for lines of:
key1: value1
key2: value2
Simple to read, simple to write.
On 12/5/2023 11:50 AM, MRAB via Python-list wrote:
On 2023-12-05 14:37, Chris Green via Python-list wrote:
Is there a neat, pythonic way to store values which are 'sometimes'Some kind of key/value store sounds like the correct solution. I
changed?
My particular case at the moment is calibration values for ADC inputs
which are set by running a calibration program and used by lots of
programs which display the values or do calculations with them.
From the program readability point of view it would be good to have a
Python module with the values in it but using a Python program to
write/update a Python module sounds a bit odd somehow.
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.
Are there any Python modules aimed specifically at this sort of
requirement?
wouldn't go as far a database - that's overkill for a few calibration values.
I might suggest TOML, except that Python's tomllib (Python 3.11+) is read-only!
Personally, I'd go for lines of:
key1: value1
key2: value2
Simple to read, simple to write.
Just go with an .ini file. Simple, well-supported by the standard
library. And it gives you key/value pairs.
Chris Green <cl@isbd.net> writes:
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.
I've used configparser for this, though its intention is files that are manually edited, rather than updated by a program.
You can also read and dump a json file or pickle file. I prefer json to pickle for most purposes these days.
I don't like YAML and I don't like the proliferation of markup formats
of this type. So while I don't know exactly what TOML is, I figure it
must be bad.
I sometimes use ast.literal_eval though it is Python specific.
Of course there is also sqlite but that is probably overkill.
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Chris Green <cl@isbd.net> writes:
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
That maps nicely to two directories with three files
(under an application-specific configuration directory).
Thomas Passin <list1@tompassin.net> wrote:
Just go with an .ini file. Simple, well-supported by the standard
library. And it gives you key/value pairs.
My requirement is *slightly* more complex than just key value pairs,
it has one level of hierarchy, e.g.:-
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Different numbers of value pairs under each KEY.
On 6 Dec 2023, at 09:32, Chris Green via Python-list <python-list@python.org> wrote:
My requirement is *slightly* more complex than just key value pairs,
it has one level of hierarchy, e.g.:-
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Different numbers of value pairs under each KEY.
On 6 Dec 2023, at 09:32, Chris Green via Python-list <python-list@python.org> wrote:
My requirement is *slightly* more complex than just key value pairs,
it has one level of hierarchy, e.g.:-
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Different numbers of value pairs under each KEY.
JSON will allow you to nest dictionaries.
{
'KEY1': {
'a': v1
'c': v3
'd': v4
}
'KEY2': {
'a': v7
'b': v5
'd': v6
}
}
Personally I would not use .ini style these days as the format does not include type of the data.
On 12/6/2023 6:35 AM, Barry Scott via Python-list wrote:
On 6 Dec 2023, at 09:32, Chris Green via Python-list <python-list@python.org> wrote:
My requirement is *slightly* more complex than just key value pairs,
it has one level of hierarchy, e.g.:-
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Different numbers of value pairs under each KEY.
JSON will allow you to nest dictionaries.
{
'KEY1': {
'a': v1
'c': v3
'd': v4
}
'KEY2': {
'a': v7
'b': v5
'd': v6
}
}
Personally I would not use .ini style these days as the format does not include type of the data.
Neither does JSON. Besides, JSON is more complicated than necessary
here - in fact, your example isn't even legal JSON since lines are
missing commas.
Fun fact - for at least some, maybe most, JSON files, using eval() on
them is hugely faster than using Python's standard JSON library. I
learned this when I wanted to ingest a large browser bookmarks JSON
file. It wouldn't matter for a much smaller file, of course.
On 2023-12-06 12:23, Thomas Passin via Python-list wrote:
On 12/6/2023 6:35 AM, Barry Scott via Python-list wrote:It would be safer if you used literal_eval.
On 6 Dec 2023, at 09:32, Chris Green via Python-list
<python-list@python.org> wrote:
My requirement is *slightly* more complex than just key value pairs,
it has one level of hierarchy, e.g.:-
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Different numbers of value pairs under each KEY.
JSON will allow you to nest dictionaries.
{
'KEY1': {
'a': v1
'c': v3
'd': v4
}
'KEY2': {
'a': v7
'b': v5
'd': v6
}
}
Personally I would not use .ini style these days as the format does
not include type of the data.
Neither does JSON. Besides, JSON is more complicated than necessary
here - in fact, your example isn't even legal JSON since lines are
missing commas.
Fun fact - for at least some, maybe most, JSON files, using eval() on
them is hugely faster than using Python's standard JSON library. I
learned this when I wanted to ingest a large browser bookmarks JSON
file. It wouldn't matter for a much smaller file, of course.
On 7/12/23 07:12, MRAB via Python-list wrote:
On 2023-12-06 12:23, Thomas Passin via Python-list wrote:
On 12/6/2023 6:35 AM, Barry Scott via Python-list wrote:It would be safer if you used literal_eval.
On 6 Dec 2023, at 09:32, Chris Green via Python-list
<python-list@python.org> wrote:
My requirement is *slightly* more complex than just key value pairs, >>>>> it has one level of hierarchy, e.g.:-
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Different numbers of value pairs under each KEY.
JSON will allow you to nest dictionaries.
{
'KEY1': {
'a': v1
'c': v3
'd': v4
}
'KEY2': {
'a': v7
'b': v5
'd': v6
}
}
Personally I would not use .ini style these days as the format does
not include type of the data.
Neither does JSON. Besides, JSON is more complicated than necessary
here - in fact, your example isn't even legal JSON since lines are
missing commas.
Fun fact - for at least some, maybe most, JSON files, using eval() on
them is hugely faster than using Python's standard JSON library. I
learned this when I wanted to ingest a large browser bookmarks JSON
file. It wouldn't matter for a much smaller file, of course.
Ah, memories of Python2...
Does this little hack still work?
What about True/False cf true/false?
On 2023-12-06 12:23, Thomas Passin via Python-list wrote:
On 12/6/2023 6:35 AM, Barry Scott via Python-list wrote:It would be safer if you used literal_eval.
On 6 Dec 2023, at 09:32, Chris Green via Python-list
<python-list@python.org> wrote:
My requirement is *slightly* more complex than just key value pairs,
it has one level of hierarchy, e.g.:-
KEY1:
a: v1
c: v3
d: v4
KEY2:
a: v7
b: v5
d: v6
Different numbers of value pairs under each KEY.
JSON will allow you to nest dictionaries.
{
'KEY1': {
'a': v1
'c': v3
'd': v4
}
'KEY2': {
'a': v7
'b': v5
'd': v6
}
}
Personally I would not use .ini style these days as the format does
not include type of the data.
Neither does JSON. Besides, JSON is more complicated than necessary
here - in fact, your example isn't even legal JSON since lines are
missing commas.
Fun fact - for at least some, maybe most, JSON files, using eval() on
them is hugely faster than using Python's standard JSON library. I
learned this when I wanted to ingest a large browser bookmarks JSON
file. It wouldn't matter for a much smaller file, of course.
On 12/6/2023 6:35 AM, Barry Scott via Python-list wrote:
Personally I would not use .ini style these days as the format does not include type of the data.
Neither does JSON.
The biggest caveat is that the shared variable MUST exist before it can
be examined or used (not surprising).
But it does not work here
under CPython.
On Wed, 27 Dec 2023 03:53:42 -0600, Greg Walters wrote:
The biggest caveat is that the shared variable MUST exist before it can
be examined or used (not surprising).
There are a few other questions. Let's say config.py contains a variable like 'font' that is a user set preference or a calibration value
calculated by A to keep with the thread title. Assuming both scripts are running, how does the change get propagated to B after it is set in A
and written to the shared file?
Then how does it help the OP to propogate clibration values from one
program to another or from one program run to the next run?
On 2023-12-28, Peter J. Holzer via Python-list <python-list@python.org> wrote:
On 2023-12-28 05:20:07 +0000, rbowman via Python-list wrote:
On Wed, 27 Dec 2023 03:53:42 -0600, Greg Walters wrote:
The biggest caveat is that the shared variable MUST exist before it can >> > be examined or used (not surprising).
There are a few other questions. Let's say config.py contains a variable >> like 'font' that is a user set preference or a calibration value
calculated by A to keep with the thread title. Assuming both scripts are >> running, how does the change get propagated to B after it is set in A
It isn't. The variable is set purely in memory. This is a mechanism to share a value between multiple modules used by the same process, not to share between multiple processes (whether they run the same or different scripts)
and written to the shared file?
Nothing is ever written to a file.
Then how does it help the OP to propogate clibration values from one
program to another or from one program run to the next run?
[-- text/plain, encoding quoted-printable, charset: us-ascii, 40 lines --]
On 2023-12-29 09:01:24 -0800, Grant Edwards via Python-list wrote:
On 2023-12-28, Peter J. Holzer via Python-list <python-list@python.org> wrote:
On 2023-12-28 05:20:07 +0000, rbowman via Python-list wrote:
On Wed, 27 Dec 2023 03:53:42 -0600, Greg Walters wrote:
The biggest caveat is that the shared variable MUST exist before it can
be examined or used (not surprising).
There are a few other questions. Let's say config.py contains a variable >> like 'font' that is a user set preference or a calibration value
calculated by A to keep with the thread title. Assuming both scripts are >> running, how does the change get propagated to B after it is set in A
It isn't. The variable is set purely in memory. This is a mechanism to share a value between multiple modules used by the same process, not to share between multiple processes (whether they run the same or different scripts)
and written to the shared file?
Nothing is ever written to a file.
Then how does it help the OP to propogate clibration values from one program to another or from one program run to the next run?
It doesn't. See his second mail in this thread, where he explains it in
a bit more detail. I think he might be a bit confused in his
terminology.
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 546 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 33:29:32 |
| Calls: | 10,391 |
| Calls today: | 2 |
| Files: | 14,064 |
| Messages: | 6,417,129 |