[Tutor] ConfigParser and multiple option names
maseriyer at yahoo.com
Tue Apr 10 09:10:59 CEST 2007
wow, Andrei, that is a really very neat solution ! It
So it seems configparser is dict based.
Thanks a lot for your help.
--- Andrei <project5 at redrival.net> wrote:
> Iyer wrote:
> > I cannot change the config file layout -- it is a
> > [dir_options]
> > dir="/home/florian"
> > dir="/home/john"
> > what method would you suggest to read each "dir"
> > 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
> 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')
> 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.
> Mail address in header catches spam. Real contact
> ''.join([''.join(s) for s in zip(
> "poet at aao.l pmfe!Pes ontuei ulcpss edtels,s hr' one
> "rjc5wndon.Sa-re laed o s npbi ot.Ira h it oteesn
> edt C")])
> Tutor maillist - Tutor at python.org
We won't tell. Get more on shows you hate to love
(and love to hate): Yahoo! TV's Guilty Pleasures list.
More information about the Tutor