Python ConfigParser module is useful
when you want to set the defaults in a configuration file and have your application read
the file, parse it and behave based on its contents.
ConfigParser takes care of the reading
and parsing part.
It accepts configuration files with structure similiar to INI files.
Some gotchas for using
SafeConfigParserdoes not make any attempt to understand the option type. The application is expected to use the correct method to fetch the value as the desired type.
get()always returns a string. Use
getfloat()for floating point numbers, and
getboolean()for boolean values.
Also, mind the
ConfigParser API differences (especially constructors) between
python 2.6 and python 2.7.
For your own interests, the value searching algorithm for those parsers
ConfigParser) is like this:
- Before starting the option search, the section name is tested. If the section
does not exist, and the name is not the special value DEFAULT, then
- If the option name appears in the vars dictionary passed to
get(), the value from vars is returned.
- If the option name appears in the specified section, the value from that section is returned.
- If the option name appears in the DEFAULT section, that value is returned.
- If the option name appears in the defaults dictionary passed to the constructor, that value is returned.
- If the name is not found in any of those locations, NoOptionError is raised.
For more details, please check out PyMOTW: