ConfigParser Help

Howard Lightstone howard at eegsoftware.com
Tue Oct 22 13:24:27 EDT 2002


Karthikesh Raju <karthik at james.hut.fi> wrote in
news:m3zntgx3v4.fsf at mycomp.tky.hut.fi: 

> 
> Hi,
> 
> i would like to get some help with ConfigParser module. Currenly say,
> i have an ini file as
> 
> [a]
> noofusers = 10
> noofsources = 20
> 
> [b]
> noofusers = 10
> noofsources = 20
> 
> currenly i do something like 
> 
> c = cfg.LoadConfig('file.ini') and i get
> 
> c -  a dictionary whos elements are
> 
> {'a.noofusers':'10','a.noofsources':'20','b.noofusers':'10','b.noofsour
> ces':'20'} 
> 
> well, i can live with this but would really be happy with variables a
> dictionary of the sections and its subsections as keys with the RHS
> the values for the keys.

Based on some Python samples I saw somewhere (Cookbook?), I took a 
different track.  I created another module (which itself calls 
ConfigParser) which does several things:

1.  Returns back the ini file contents as a dictionary with (lower-case)  
keys  'sectionname.optionname' whose values are the option values.  That 
way, I could look up a setting with one reference.

2.  Has a function to invert the dictionary into another dictionary 
whose keys are the section names and whose key values are the respective 
option names (like ConfigParser but from this dictionary structure).  This 
allows me to look up all the related option settings.

3.  Can rewrite the ini file and KEEP the case of the section/option names 
from the original ini file AND retain several flavors of remarks from the 
original option file.

It is the third item that drove me to do this since (poor memory aside) I 
like having comments (at least to myself) in ini-style files to document 
what the options mean.

I would post this but (embarassingly) I wrote it in non-Pythonic fashion 
and never got around to 'fixing' it up.  If you would like a copy, I would 
be happy to send it to you.



More information about the Python-list mailing list