Message190767
The configparser.RawConfigParser class implements some `get` methods:
get, getint, getfloat, getboolean
but if any of these get overridden on a subclass(with other arguments) or new ones are added (e.g. getlist), there's no way a SectionProxy instance will be able to use them.
class DemoParser(ConfigParser):
def getlist(self, section, option):
return self.get(section, option).split()
parser = DemoParser()
parser.read(some_file)
# These 2 lines should be equivalent, like "getint", but the
# second doesn't work because of the SectionProxy instance
parser.getlist('some_section', 'foo')
parser['some_section'].getlist('foo')
Reading the code, for SectionProxy, it redefines all the get* methods from RawConfigParser, and that looks pretty bad...
A more elegant approach would be to fetch the function on the parser instance and bound to the section name with `lambda` or `functools.partial`... Something like:
class SectionProxy(...):
...
def __getattr__(self, attr):
if not attr.startswith('get'):
raise AttributeError(attr)
fn = getattr(self._parser, attr)
return lambda *args, **kw: fn(self._name, *args, **kw) |
|
Date |
User |
Action |
Args |
2013-06-07 17:43:50 | JBernardo | set | recipients:
+ JBernardo |
2013-06-07 17:43:50 | JBernardo | set | messageid: <1370627030.12.0.961792763365.issue18159@psf.upfronthosting.co.za> |
2013-06-07 17:43:50 | JBernardo | link | issue18159 messages |
2013-06-07 17:43:49 | JBernardo | create | |
|