Author lukasz.langa
Recipients JBernardo, lukasz.langa
Date 2013-06-25.23:21:54
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1372202514.95.0.554098281936.issue18159@psf.upfronthosting.co.za>
In-reply-to
Content
There are several reasons why `get*()` methods are redefined on the section proxy. First of all, explicit is better than implicit. Secondly, the order of arguments is different: `parser.get()` has the fallback argument as the last (and keyword-only), whereas `parser['section'].get()` behaves like a mapping. You can do `parser['section'].get('key', 'some-fallback-value')` and `parser['section'].get('no-such-key')` returns None instead of raising `NoOptionError`.

This makes it difficult to automagically support parser `get*()` methods on the section proxy. This is why I decided to adapt a different mechanism: register a converter and a getter is automatically available:

  >>> cp = ConfigParser()
  >>> cp.converters['list'] = lambda value: value.strip().split()
  >>> cp.getlist('section', 'l')
  ['a', 'b', 'c']
  >>> cp['section'].getlist('l')
  ['a', 'b', 'c']
  >>> cp.getdict('section', 'd')
  Traceback (most recent call last):
  ...
  AttributeError: 'ConfigParser' object has no attribute 'getdict'
  >>> cp['section'].getdict('d')
  Traceback (most recent call last):
  ...
  AttributeError: 'ConfigParser' object has no attribute 'getdict'

This ensures that you can easily add new converters in subclasses or single instances and that the parser-level API and section-level API work like they should. This also makes implementing custom getters easier since there's no logic involved besides the conversion. And if you happen to need custom logic anyway, you can register a converter that is a callable class.
History
Date User Action Args
2013-06-25 23:21:55lukasz.langasetrecipients: + lukasz.langa, JBernardo
2013-06-25 23:21:54lukasz.langasetmessageid: <1372202514.95.0.554098281936.issue18159@psf.upfronthosting.co.za>
2013-06-25 23:21:54lukasz.langalinkissue18159 messages
2013-06-25 23:21:54lukasz.langacreate