• How/where to store calibration values - written by program A, read by p

    From Chris Green@21:1/5 to All on Tue Dec 5 14:37:15 2023
    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?

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Chris Green on Tue Dec 5 15:47:49 2023
    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" )

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Chris Green via Python-list on Tue Dec 5 16:50:06 2023
    On 2023-12-05 14:37, Chris Green via Python-list wrote:
    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?

    Some kind of key/value store sounds like the correct solution. I
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Chris Green on Tue Dec 5 11:52:24 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Scott@21:1/5 to All on Tue Dec 5 20:45:36 2023
    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?

    I tend to use JSON for this type of thing.
    Suggest that you use the options to pretty print the json that is saved so that a human can read it.

    For example:
    ----
    import json

    config = {
    'key2': 42,
    'key1': 'value 1',
    }

    print(json.dumps(config, indent=4, separators=(', ', ': '), sort_keys=True))

    ----
    % python $T/a.py
    {
    "key1": "value 1",
    "key2": 42
    }

    Barry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From DL Neil@21:1/5 to All on Wed Dec 6 10:08:12 2023
    Apologies: neglected suggested web.refs:

    https://datagy.io/python-environment-variables/ https://pypi.org/project/json_environ/

    --
    Regards =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mats Wichmann@21:1/5 to Chris Green via Python-list on Tue Dec 5 13:30:53 2023
    On 12/5/23 07:37, Chris Green via Python-list wrote:
    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?

    A search term to look for is "data persistence"

    there is lots of support at various levels - you can do simpler things
    with plain text (or binary), json data, or csv data, or configparser, or
    use pickles; if there's not a lot of values a dbapi database may, as
    already mentioned, be overkill.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From DL Neil@21:1/5 to Chris Green via Python-list on Wed Dec 6 09:54:38 2023
    On 12/6/23 03:37, Chris Green via Python-list wrote:
    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?

    Another programming-term for these might be "environment variables".
    However, be aware that such also has a specific meaning at the Operating
    System level.

    1 Sysops Environment Variables exist completely outside the code. Python interrogates the Sysops to fetch/set them. Can be problematic because
    only apply on single machine and are not part of change-control, VS, etc.

    2 A .con file (in my tradition, likely still .uni type in MSFT) or
    similar, which contains the key:value pairs recommended elsewhere. There
    are formal .con and .uni (etc) formats. Most of the teams I've
    worked-with recently seem to settle on .JSON files which are
    very-conveniently structured as (read into/written from) a Python
    dictionary, and a single function-call interaction.

    Word of warning/voice of [bitter] experience: ALWAYS *trumpet* any
    changes in these values AND output them (as you would any "assumptions"
    for a calculation) at the top of output reports. The trouble is that
    humans assume continuity but such an arrangement is NOT idempotent -
    which leads to complaints: "I ran it on Monday and got these results,
    but when I ran it again on Tuesday, the results changed"...

    Yes there are Python libraries. Choose your method/format first, and
    then search - either Duckboards or straight from Pepi.

    I have systems which use an DBMS for environment variables, but (a) only
    when there's a significant number, and (b) when the application is
    already connecting to the DBMS for processing [and maybe (c) because I
    know my way around such tools so they're 'easy']. Not recommended!

    --
    Regards =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to MRAB via Python-list on Tue Dec 5 18:56:14 2023
    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'
    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?

    Some kind of key/value store sounds like the correct solution. I
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Thomas Passin on Wed Dec 6 09:32:02 2023
    Thomas Passin <list1@tompassin.net> wrote:
    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'
    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?

    Some kind of key/value store sounds like the correct solution. I
    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.

    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.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to Paul Rubin on Wed Dec 6 09:45:21 2023
    Paul Rubin <no.email@nospam.invalid> wrote:
    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.

    That's interesting, I'll add it to my armoury anyway. :-)


    Of course there is also sqlite but that is probably overkill.

    It's what my current code uses but does feel a bit OTT and it isn't particularly convenient to view when debugging.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Chris Green on Wed Dec 6 09:55:46 2023
    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).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Green@21:1/5 to All on Wed Dec 6 09:48:45 2023
    Thank you everyone for all the suggestions, I now have several
    possibilities to follow up. :-)

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dan Purgert@21:1/5 to Stefan Ram on Wed Dec 6 11:06:25 2023
    On 2023-12-06, Stefan Ram wrote:
    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).

    Or an .ini file with two sections (although I don't think you can re-use key-names in a single ini file)


    --
    |_|O|_|
    |_|_|O| Github: https://github.com/dpurgert
    |O|O|O| PGP: DDAB 23FB 19FA 7D85 1CC1 E067 6D65 70E5 4CE7 2860

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From 2QdxY4RzWzUUiLuE@potatochowder.com@21:1/5 to Chris Green via Python-list on Wed Dec 6 05:40:50 2023
    On 2023-12-06 at 09:32:02 +0000,
    Chris Green via Python-list <python-list@python.org> wrote:

    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.

    INI files have sections.

    See <https://en.wikipedia.org/wiki/INI_file#Sections>.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Scott@21:1/5 to All on Wed Dec 6 11:35:04 2023
    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.

    Also I would not use the ast.literal_eval as it makes debugging errors in the data harder.

    Barry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Barry Scott via Python-list on Wed Dec 6 07:23:51 2023
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Thomas Passin via Python-list on Wed Dec 6 18:12:20 2023
    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:


    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.

    It would be safer if you used literal_eval.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to MRAB via Python-list on Thu Dec 7 09:11:55 2023
    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:


    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.

    It would be safer if you used literal_eval.

    Ah, memories of Python2...

    Does this little hack still work?

    What about True/False cf true/false?

    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to dn via Python-list on Wed Dec 6 20:32:44 2023
    On 2023-12-06 20:11, dn via Python-list wrote:
    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:


    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.

    It would be safer if you used literal_eval.

    Ah, memories of Python2...

    Does this little hack still work?

    What about True/False cf true/false?

    Nope, nor None cf null.

    If it's numbers, strings, lists and dicts, it works.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to MRAB via Python-list on Wed Dec 6 15:31:44 2023
    On 12/6/2023 1:12 PM, 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:


    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.

    It would be safer if you used literal_eval.

    He's going to be writing his own calibration data files, though, so it
    should be safe for his purposes.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Thomas Passin via Python-list on Sat Dec 9 10:59:24 2023
    On 2023-12-06 07:23:51 -0500, Thomas Passin via Python-list wrote:
    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.

    Well, it distinguishes between some primitive types (string, number,
    boolean, null) and provides two container types (dict/object,
    list/array). As long as those types are sufficient, JSON includes them.
    If you need anything else, you're on your own.

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmV0OncACgkQ8g5IURL+ KF1IKRAApB7p6Xk8T7UJfEmq6v2gshuTVRy0vUOKgJrFquLp/J9uMk7HHrVZyAVH ECK89UuTCx0WcJHvf+PyS2leIuua2tEX27uqFXo8YAm13NuiQYQ8kJwxeRFSui+N w5+aVAyTd9orfsT3phfRkGl4gOaXAijz9pocvbqsGLWDd41yYWnOJvIjGquXGNZK 3bUUz2G7sK0KhnF0gSKzzgR6Zyph6lhCcSsk52akn9HKbQWdef+l5EQxYFbHFz63 l3tJbOqlBS9+WfN5nAYlyP3aYTyRrSuqKDF3RD9LbZ2oc7og9TaMMQez96V47kLA 5kkpZJ06U/34CAYuJAsL5kop9/rSkXX8vNOBpoe1hWSLga4DG3upsl7iyR48HfP2 gbLPUrKKvFF75OA77JUyIxssyjW5p9ulns+u51nWf5llk0Mn1RlGsO+5YI20Y0fL 2ROQhxs2HBi84vXpmT+sqIt3dS3cr/1OXHsKsdZwxcpGHkCSmB6pbPFVjC7RxhQ2 v8IHU5d512BbKVRjUoL4vZw2Eh1lck9wOGsm8nJvQRb9fc4YOgyhc9ye8EMyiHJP FJKB7xTWMqoxq6U/GR4tekLH0WY0SeiTfDAgU+71mE7RWUE/dMP1B3yA7e1g495u 8m1EjEGQS9qiLwnYTJ4qUcSQE/wv/nK9s/nvTYL
  • From rbowman@21:1/5 to Greg Walters on Thu Dec 28 05:20:07 2023
    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? Is there a mechanism to allow both scripts to
    make updates?

    The easy way out is to assume the changes will be picked up when the
    scripts are restarted but this is not always acceptable.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to rbowman on Thu Dec 28 11:38:37 2023
    rbowman <bowman@montana.com> writes: 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?

    I don't think it was intended to be written to the file,
    just to the module in memory. But it does not work here
    under CPython.

    config.py

    value = None

    sender.py

    import config
    import itertools
    import time

    for i in itertools.count():
    config.value = i
    time.sleep( 1 )

    receiver.py

    import config
    import time

    while True:
    print( config.value )
    time.sleep( 1 )

    output

    None
    None
    None

    I tried it with sockets. But due to my lack of experience with
    sockets, it might be more complicated and erroneous than necessary!
    It needs some more work before it can be used in a product.

    receiver.py (start this one first)

    import socket as socket_module

    server = socket_module.socket()
    server.bind( ( '127.0.0.1', 58962 ))
    server.listen()
    client, address = server.accept()
    result = b''

    while True:
    while len( result )< 128 and b'\r\n' not in result:
    result += client.recv( 128 )
    while b'\r\n' in result:
    txt, result = result.split( b'\r\n', 1 )
    if txt:
    print( int( txt ))

    client.close()

    sender.py

    import itertools
    import socket as socket_module
    import time

    socket_module.setdefaulttimeout( 5 )

    socket = socket_module.socket()
    socket.connect( ( '127.0.0.1', 58962 ))
    for i in itertools.count():
    socket.send( bytes( str( i ), encoding='ASCII' ) + b'\r\n' )
    time.sleep( 1 )
    socket.close()

    output

    0
    1
    2

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Thu Dec 28 13:17:06 2023
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    But it does not work here
    under CPython.

    Sorry! It was meant to be shared between
    different modules in the /same process/.
    I instead tried to use it between different
    modules in /different processes/!

    a.py

    import config
    config.value = 22
    import b

    b.py

    import config
    print( config.value )

    output

    22

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to rbowman via Python-list on Thu Dec 28 15:29:42 2023
    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.

    You could of course write python files from a python script (in fact I
    do this), but that's not what this pattern is about, AFAICS.

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmWNhk4ACgkQ8g5IURL+ KF2h4BAAqxhE/LIEro/i3Fcvsu6vUG1EjYmA3mMKoREh1ZmCrGKOTrYTsdfyC5WZ Otxp2SVbGloBEx8CBeIttvrgCKYx1AK6iTYkR3YQOuyfDx1gEoUlhnxsCTqQg365 cS8BCJv2+jgUb/DQUgpKKa9qnONNeGNNrGQfAiiun1S9N91AGlTjFiYs8FB7cZR+ 2bXjqq+h/g+hxLJyEas/NAscYhhfE9HUbW7Lu+7g2dDqbPgv/dm/C50yp4QCYCZE U6olRgEieg3wg4mTN9Zpea+GLPhWrTqQ1IgCQZ0/0UJVlJSkjR1hnZO/NKVdnSXT nrOWeXwdHNQbtgQJqkkTAx2wGN5NrV8LE5LRkDRnFE70KLOXN9hqD/e+ixNJrDxh XHdcGy89MwxND5nsE5WFp/1zyLOHfV6Us97EJqljlDgjocEkf8ZTzXxCI9/4rzMt rSmE+k61Xhr2wkBDXuLWEnYfL8C9OwY9XoLffHixiXdXnSURZdZH4hH5M9Chp3+1 uM6EmoDQlW480JwzxWNje8QI7ddwEXvzIjMnl1I6pGsr2cN3oKueBHKrxltLA/VP kE5uYAdT6LuOPGxhNp9rudfBe5jGNvwtZkUyl7vE/y5trB805hZkZL5k5hL9r7hx dMs6wnt7R2LdPYjFNrQlXsaxI34j2KNJfV8CUaA
  • From Richard Damon@21:1/5 to All on Thu Dec 28 10:49:52 2023
    On 12/28/2023 12:20 AM EST rbowman via Python-list
    <[1]python-list@python.org> 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
    and
    written to the shared file? Is there a mechanism to allow both scripts
    to
    make updates?

    The easy way out is to assume the changes will be picked up when the
    scripts are restarted but this is not always acceptable.

    --
    [2]https://mail.python.org/mailman/listinfo/python-list

    If one module does a:

    import config

    config.font = "New Value"

    Then every other module in that program that also did a

    import config

    will see the new value of that variable, as the assignment rebound the
    name in the module namespace to the new value.

    Note, it does NOT work if you did a

    from config import font

    font = "New Value"



    as that doesn't change the binding in the config module.

    IF you need to propagate to a different process, you need something
    different.


    References

    Visible links
    1. mailto:python-list@python.org
    2. https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Grant Edwards on Fri Dec 29 17:33:03 2023
    Grant Edwards <grant.b.edwards@gmail.com> writes:
    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?

    One could also use files. But I'm not sure whether a locking scheme
    would be required when there are concurrent accesses by different
    processes. Maybe it's okay as long as there are never concurrent
    writes. Otherwise, one could try to use SQLite for the locking.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Grant Edwards via Python-list on Sat Dec 30 11:30:36 2023
    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.

    hp

    --
    _ | Peter J. Holzer | Story must make more sense than reality.
    |_|_) | |
    | | | hjp@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmWP8UQACgkQ8g5IURL+ KF3HYA/+N1MdmTBFICtnVMtp3HiHjSHTC8ccfn8/EpJbNAzbRHNaQFMTaxQyq0J2 GlBFX7S22fcxcIK3NwIUTFQnipIHXje4dKCDdU2gEFSVSyDjmoH/n7z4AiR+MTc6 Uj8yP1IOkBf35AXfD1B/RK5AoOlXrjk4jxpdA6rV2ufUXMce6JvMkk17l1jOLeXt dRljUWivrcQtca684Mq2eGM2SfxbSCyHHHf2TQrZEwY7Vb6gNvAGYgXS3scuNAUM ex66qLzrXXkF0LcZBdLDl/qEvrEJZYtRuhDc2PRrvWo7dKvm39Ff7DHtd+nN9mZk +lxThY5jkSoz9lZ2qkAaO7eo6IWGzFZWgbA1PEB50xFxWa/HfQuGJVlwZma0awoo yBwGOni+vjBMK+1/rvIi7Y+TM8Qt2UrGBjraWWJKn6Op2jU+ZFsjnr0R2wsabmFp XhBL31sg8J6WYoTA+W0Gip9HpnBHY3iIm6QIpTxl+dGK0Z7hpojkcjqM+eHC0obj xfbb+XWwtNnvJS6fkt1cUMB4NGBFzPwGYEM25uotp5fVHfA5/XWKFTsb+vp4FiC8 8FAPkijsx2uIkhHiGAQLo/wkPka6Q1YwyhXCLD0mbQVlaZ7Zg37Dt+DyghKX6Zrd LL3Uxv7Xn4dlCWKfFiyddVeGKw/NJDFSo4AXE+Q
  • From Chris Green@21:1/5 to Peter J. Holzer on Sat Dec 30 11:05:31 2023
    Peter J. Holzer <hjp-python@hjp.at> wrote:
    [-- 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.

    If I am the OP (I suspect I may be) I have gone with JSON stored in a
    file to provide what I need. The Python json package is very simple
    to use and with an 'indent=' setting the resulting json is reasonably
    human readable which is all I need.

    Thus programs simply read the values from the json file into a
    dictionary of dictionaries and the 'updater of values' program can
    write them back after changes.

    --
    Chris Green
    ·

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)