ConfigParser shootout, K.I.S.S. entry.

David Wilson dw at botanicus.net
Thu Oct 21 16:39:41 EDT 2004


Greetings Python-List,

    http://botanicus.net/dw/tmp/ConfigParser2/ConfigParser.py


The above implementation of 'ConfigParser2' takes a radically different
approach to that of the good Mr. Chermside's entry, or any of the
so-called proposed 'enhancements' to ConfigParser. It does this by
*removing* features which I believe do not belong in ConfigParser, and
repairing the existing class interface.

Please let me know what you think, CC: me if possible. This module,
although unused (and only an hour or three old), will probably live in
my personal toolkit from now on. Its interface is superior to that of
ConfigParser's in many ways, despite being less than 51% of the size of
it's predecessor:


Uses scaleable, built-in functionality where applicable:

    - Exceptions. Don't be afraid of the built-in exceptions. Don't even
      bother to raise them if you don't need to. Any Python programmer
      worth his salt will know exactly what a KeyError means, regardless
      of context.

    - Type factories. Why provide helper methods for type conversion at
      all? That method does not scale very well, and could possibly
      break more easily with newer Python versions. In fact, it is
      pointless.

    - Use iterators. Everyone knows that Python 2.4 comes with generator
      expressions. There has been talk for a long time of having
      co-routines and such things in the core language. Perhaps it will
      never happens, but one thing is for sure: iterables are in
      fashion.

      This module uses iterables where appropriate, to save memory, to
      save copies, and to save lines of code. Certain operations may
      seem strange to you. For example,

        items = conf.items('some section')

      'items' contains an iterator, not the list of items that you might
      expect. Be explicit in what you are asking for:

        items = list(conf.items('some section'))

      Perhaps you don't need the list after-all:

        for key, value in conf.items('some section'):
            ...

      Would you like a dictionary? Swap 'list' for 'dict' above. It is
      all very simple, which is the point. :)


    - Method names sound more like their built-in type counterparts, for
      example, append(), set(), get(), pop(), has_key(), etc.


    - Uses unicode throughout when loading and saving configuration
      files, although awareness of it completely optional. Again, this
      is compatible with the target of unicode() becoming the default
      string type in some future version of Python.


    - At present, expects the reader to exercise 'pydoc' or help() to
      access documentation, which is suffice for me at least. There is
      no strange behaviour that needs documentation.



I would like to take this moment to ask all those proposing massive
changes to ConfigParser to do so under a different name - ConfigParser
is not the place for your code. Please do not use 'config' either, it is
too ambiguous. Thanks.


David.

--
Astrology is a load of bollocks. But then, as a Leo, I'm naturally
sceptical.
    -- Post to http://talk.guardian.co.uk/



More information about the Python-list mailing list