[Tutor] ConfigParser and multiple option names
Iyer
maseriyer at yahoo.com
Tue Apr 10 09:10:59 CEST 2007
wow, Andrei, that is a really very neat solution ! It
worked.
So it seems configparser is dict based.
Thanks a lot for your help.
-iyer
--- Andrei <project5 at redrival.net> wrote:
> 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")])
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
____________________________________________________________________________________
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.
http://tv.yahoo.com/collections/265
More information about the Tutor
mailing list