[Python-Dev] ConfigParser shootout, preliminary entry

Johannes Gijsbers jlg at dds.nl
Thu Oct 21 22:47:22 CEST 2004

On Tue, Oct 19, 2004 at 08:00:23AM -0700, Guido van Rossum wrote:
> Let me suggest two variations that I have used successfully in my day
> job (which is also my night job :).
> 1. For parsing .ini files, I wrote a wrapper around ConfigParser. The
> Python-level API looks like this (anonymized and idealized):
> from XXX import MyConfigWrapper, optstr, optint, optbool, optfloat
> class Config(MyConfigWrapper):
>     poll_time = optfloat("network-parameters", "poll-time")
>     use_ssl = optbool("network-parameters", "use-ssl")
>     window_title = optstr("ui-parameters", "window-title")
>     # etc.

I'm surprised no one has mentioned optparse yet. It already has all the
features you use in this example. Maybe a similar API for configuration file
parsing would be nice, if only for the sake of consistency:

  parser = ConfigParser()
  parser.add_option("network-parameters", "poll-time", type="float", 
  parser.add_option("network-parameters", "use-ssl", type="bool", 
  parser.add_option("ui-parameters", "window-title", type="float", 
  options = parser.parse_file('foo.conf')

  print options.window_title
  print options.use_ssl
  # etc.

Bonus points if the implementation allows me to specify a command-line option
and configuration file option with one call, as in docutils [1]. More bonus
points for reusing optparse code.



[1] http://cvs.sourceforge.net/viewcvs.py/docutils/docutils/docutils/frontend.py?rev=HEAD&view=auto

More information about the Python-Dev mailing list