[Tutor] ConfigParser and multiple option names
Andrei
project5 at redrival.net
Sat Apr 7 10:22:07 CEST 2007
Iyer wrote:
<snop>
>
> I cannot change the config file layout -- it is a
<snip>
> [dir_options]
> dir="/home/florian"
> dir="/home/john"
<snip>
> what method would you suggest to read each "dir" value
> above from the config_file.lay?
Internally, the ConfigParser implementation uses dictionaries to store
sections and section contents making it impossible to have duplicate
sections or option names - this file is against the philosophy of the
parser.
If these dir values are all you're interested in, you could write a very
simple parser only for them in a few lines.
Alternatively I think you'll have to trick the parser into thinking
these are different options by overriding optionxform, to mangle the
option name and make it unique:
>>> from random import random
>>> class DuplicateOptionParser(ConfigParser):
... def optionxform(self, option):
... return option.lower() + '____' + str(random()) + str(random())
...
>>> parser = DuplicateOptionParser()
>>> parser.read('xyz.ini')
>>> parser.items('dir_options')
[('dir____0.2893357144260.69151552211', '"/home/whoever"'),
('dir____0.8258732656650.272559810163', '"/home/florian"'),
('dir____0.6565224032210.703769464586', '"/home/john"')]
Note that it will do this for *all* options in your file, even those
that have no duplicates. You'll have to trim the part starting with
'____' before using them.
--
Yours,
Andrei
=====
Mail address in header catches spam. Real contact info:
''.join([''.join(s) for s in zip(
"poet at aao.l pmfe!Pes ontuei ulcpss edtels,s hr' one oC.",
"rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn edt C")])
More information about the Tutor
mailing list