Author JBernardo
Recipients JBernardo
Date 2013-06-07.17:43:49
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1370627030.12.0.961792763365.issue18159@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2013-06-07 17:43:50JBernardosetrecipients: + JBernardo
2013-06-07 17:43:50JBernardosetmessageid: <1370627030.12.0.961792763365.issue18159@psf.upfronthosting.co.za>
2013-06-07 17:43:50JBernardolinkissue18159 messages
2013-06-07 17:43:49JBernardocreate