[Tutor] Re: reading configuration file

Andrei project5 at redrival.net
Tue Nov 11 07:52:08 EST 2003


Eur van Andel wrote on Tue, 11 Nov 2003 11:07:45 +0100:

Hi,
> I've made a prog that reads a configuration file. While it works, I think it is
> butt-ugly and defintely not the way it should be done in Python.

You should look at the ConfigParser module.

> I tried to understand dictionaries and I tried to make eval() work. I failed. 
> What is the good Python way to do what I do below?

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

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

<snip>
> # 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.>,
               }

<snip>


-- 
Yours,

Andrei

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