Efficient configuration storage

Larry Bates lbates at swamisoft.com
Wed Sep 22 13:10:26 EDT 2004


ConfigParser is VERY efficient.  I have a program that reads
and parses thousands of configuration parameters in sub-second
times.

The methods are there to get strings, boolean (supports 0/1,
Y/N, Yes/No, etc.), integer, float.

I wrap the calls in try: blocks so I can catch bad config
parameters early in the process.

section="email"
option="emailtoaddress"

try: emailtoaddress=INI.get(section, option)
except:
    #
    # This is not fatal just set to None
    #
    emailtoaddress=None


section="init"
option="numberofcopies"
try: numberofcopies=INI.getint(section, option)
except: numberofcopies=1

option="debug"
try: _debug=INI.getboolean(section, option)
except: _debug=0

You get the idea.

Larry Bates



"sebsauvage" <gg20040922.5.sebsauvage at spamgourmet.com> wrote in message 
news:7e8c2ebd.0409220632.469b1fb6 at posting.google.com...
> Hello.
>
> In one of my programs ( http://sebsauvage.net/python/webgobbler/ ),
> I have a global dictionnary containing the whole program configuration.
> Sample follows:
> CONFIG = { "network.http.useproxy" : True,
>           "network.http.proxy.address": "proxy.free.fr",
>           "network.http.proxy.port"    : 3128
>           [etc.]
>          }
>
> I'd like to be able to save/load to/from a file.
>
> I do not want to use pickle because I want configuration to be human 
> readable.
>
> ConfigParser could do the trick, but the biggest trouble is that it does
> not retain type (boolean, integer, string...).
>
> Would I have to store everything as text and cast it everywhere
> it's used (and try/except each cast of course) ?
> 'Looks ugly and inefficient to me.
>
> Or have a configuration class which knows the type of each parameter
> and casts appropriately from the configuration file ?
>
>
> Is there a better way of doing this ?
>
> --
> Sébastien SAUVAGE
> http://sebsauvage.net 





More information about the Python-list mailing list