[Tutor] Re: reading configuration file

Eur van Andel eur at fiwihex.nl
Tue Nov 11 11:06:09 EST 2003


On Tue, 11 Nov 2003 13:52:08 +0100, Andrei <project5 at redrival.net> wrote:

>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.

>A dictionary would indeed be a good way to make it elegant. A dictionary 
>stores a value for each unique key. A key is a configuration setting in 
>your case. Keys are a bit like variables: you can't have two variables with 
>the same name, but different values, but you can have different variables 
>with the same value.
>
>> config file:
><snip>
>> CO2_soll		 80
>> pump_1_manual_override    0  
>> pump_1_manual_speed     120
><snip>
>
>> code:
><snip>
>> import sys
>> import os
>> from time import strftime, localtime, sleep
>> import random, fpformat
>> from xwisp_fwx3 import fwx
>> import string
>
>Don't use the string module. Use string methods instead, e.g. instead of:
>    string.split("bla bla")
>do:
>    "bla bla".split().
Ive seen that. Now I understand it. 

>---code (untested)---
># here's an alternative
># first, define an empty settings dictionary
>settings = {}
># open the config file
>configfile = file("somefile.cfg", "r")
># read line by line using "for line in <file>"
>for line in configfile:
>    # only read config setting if line is not
>    # empty and does not start with "#"
>    if line and line[0]!="#": 
>        # split in key-value pairs using string method
>        key, value = line.split()
>        # save them in dictionary; convert value to 
>        # integer (assuming you have only integers stored,
>        # otherwise you'll need more intelligence here
>        settings[key] = int(value)
># close file
>configfile.close()
>--------
>
>Now the settings dictionary looks like this:
>
>    {"pump_1_manual_override": 0, "CO2_soll": 80, 
>     "pump_2_manual_speed": 130, <... etc.>}
>
>You can access a single setting from it using square brackets with in 
>between them the key you want (like you do with lists), like this:
>
>    print settings["pump_1_manual_override"]
>
>will print 0.
I understand.

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"])

?

>> # initialisation of global variables 
>
>You can initialize the key-value pairs in the dictionary too (doesn't 
>change the loading method mentioned above), like this:
>
>    settings = {"CO2_soll": 81, 
>                "pump_1_manual_override": 1, 
>                "pump_1_manual_speed": 121,
>                <... etc.>,
>               }
>
>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. 


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

--
Ir. E.E. van Andel, Fine Wire Heat Exchangers, Fiwihex B.V. www.fiwihex.com
Wierdensestraat 74, NL-7604 BK  Almelo, The Netherlands   eur at fiwihex.nl
phone +31-546-491106  fax +31-546-491107  mobile +31-653-286573  



More information about the Tutor mailing list