[Tutor] Re: Re: reading configuration file

Andrei project5 at redrival.net
Tue Nov 11 11:33:56 EST 2003

Eur van Andel wrote on Tue, 11 Nov 2003 17:06:09 +0100:

>>You should look at the ConfigParser module.
> I did. I couldn't understand. Karl Fast pointed me to an example that is much
> enlightening. Examples are hard to come by, in Python. The cookbook is nice.

True for some topics. But usually it helps a lot to play with the module
you don't understand at the Python interactive prompt. If you use a good
one (like PyCrust which is included in wxPython), you get all kinds of tips
and auto-completion to help you figure it out.

> So if I want to send a msg (to a chain of PIC controllers) with the format:
> Send( addr, cmd, data3, data2, data1, data0)
> I should use:
> Send (1, set_CO2_level, settings["CO2_soll"], settings["pump_1_max"], 
>    settings["pump_2_max"], settings[pump_1_manual_override"])

I don't know what PIC controllers are, but the syntax looks OK.

>>I would also recommend *not* hard-coding those strings, but defining a 
>>number of constants (use uppercase for these constants) to use everywhere 
>>instead of typing the whole string:
>>    CO2SOLL = "CO2_soll"
>>    PUMP1MAN = "pump_1_manual_override"
>>    <... etc.>
>>Then you can write this:
>>    settings = {CO2SOLL: 81, 
>>                PUMP1MAN: 1, 
>>                <... etc.>,
>>               }
> Yeah, well the names are explicit so the config file is human readable.
> These names are used in the PIC controllers too. So I rather not add yet
> another set of vars. 

Use whatever works for you :). Note that defining those string constants
does *not* influence the data or the config file, only the amount you type
to get to that data. E.g. if you'd define your dictionary using the
constants above and you'd print it, it would still show this:

    {"CO2_soll": 81, "pump_1_manual_override": 1, <... etc.>}

NOT this:

    {CO2SOLL: 81, PUMP1MAN: 1, <... etc.>}

Another difference is that if at some point you decide to e.g. change all
pump_1_manual_override to pump_2_manual_override and the other way around,
you have a single-point change to make if you use the string constants,
while otherwise you'll have to run three global replaces. Generally
speaking, I don't think it's wise to have overly long variable names or
string constants.

> PS. Why can't I just reply to the list? Now I have to set the reply address
> manually to tutor at python.org

Is this a general problem or with my post only? I read the list and post to
it using the newsgroup interface offered by Gmane.org, so I don't really
notice any problems.



Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V
ernq gur yvfg, fb gurer'f ab arrq gb PP.

More information about the Tutor mailing list