Parsing environment variables in ConfigParser files

Peter Otten __peter__ at web.de
Fri Dec 19 17:15:28 EST 2003


Robert Brewer wrote:

> Does this snippet from ConfigParser.py give anyone any ideas on how to
> do this without subclassing? ;)
> 
> class RawConfigParser:
>     def __init__(self, defaults=None):
>         self._sections = {}
>         if defaults is None:
>             self._defaults = {}
>         else:
>             self._defaults = defaults
> 
>     def defaults(self):
>         return self._defaults

The RawConfigParser does not do interpolation.

>>From the docs: "Default values can be specified by passing them into the
> ConfigParser constructor as a dictionary. Additional defaults may be
> passed into the get() method which will override all others."

I would have expected a quote with at least one "%" character ;)

If you want to allow both $HOME and %(HOME)s style interpolation, overriding
ConfigParser.get() is the solution with the least effort. Both replacements
can then be applied orthogonally. Environment variable names are not
controlled by your application, so I would oppose injecting them into the
default section.

> Let's not patch in a single use case when a simple generic solution
> already presents itself.

I tend to agree.

Peter





More information about the Python-list mailing list