help: ConfigParser.optionxform

David Goodger goodger at python.org
Mon Mar 3 23:48:06 EST 2003


Vivek Sawant wrote:
> Can someone please help me figure out how to use the 'optionxform'
> method for ConfigParser Class?
> 
> From the documentation, it sounds like this method is supposed to allow
> me to change the default behavior of returning an 'all lower case'
> option name. For example, I want to make it so that the option name
> returned is exactly as it appears in the input file. I have no clue how
> to actually achieve this. I googled to find examples of usage of this
> method. No luck.

The library reference for ConfigParser.py says

    optionxform(option)
        Transforms the option name option as found in an input file or
        as passed in by client code to the form that should be used in
        the internal structures. The default implementation returns a
        lower-case version of option; subclasses may override this or
        client code can set an attribute of this name on instances to
        affect this behavior. Setting this to str(), for example,
        would make option names case sensitive.

The default implementation is:

    def optionxform(self, optionstr):
        return optionstr.lower()

If you don't want the optionstr lowcased in your subclass of ConfigParser,
just return it untouched:

    def optionxform(self, optionstr):
        return optionstr

Use the source, Luke!

-- 
David Goodger    http://starship.python.net/~goodger    Projects:
  * Python Docutils: http://docutils.sourceforge.net/
    (includes reStructuredText: http://docutils.sf.net/rst.html)
  * The Go Tools Project: http://gotools.sourceforge.net/





More information about the Python-list mailing list