[Python-Dev] ConfigParser shootout, preliminary entry

Carlos Ribeiro carribeiro at gmail.com
Mon Oct 18 15:28:39 CEST 2004


On Sun, 17 Oct 2004 15:10:44 -0700, Michael Chermside <mcherm at mcherm.com> wrote:
> A few weeks ago, the suggestion was made on Python-Dev that it might be time
> to consider replacing the ConfigParser module and that we should hold a
> "shootout" (ie ask for implementations and see what we get).

Now that we are in 'shootout mode', let me plug my own solution :-)
I've written a generic data strucuture templating package, and one of
the samples is a ini-file reader. The syntax is elegant and simple. It
allows one to define the expected INI file structure, using a class
definition. The class is then able to read itself from the INI file.
I've not yet implemented the write code, but it should be a snap.
Check this sample:

class WebServerIni(IniFile):
    class server(IniSection):
        socketPort = TypedAttribute(8080)
        threadPool = TypedAttribute(10)
    class staticContent(IniSection):
        bitmaps = TypedAttribute('c:/work/bitmaps')
    class session(IniSection):
        storageType = TypedAttribute('ram')

Attributes may be 'generic' (untyped) or 'typed', and are stored in
the order they are declared in the class statement. The value provided
in the definition above is the 'default value'. Simple attributes may
also be provided, but won't be type checked or kept in the original
order. To read the ini file, just do this:

inifile = WebServerIni()
inifile.load(<optional-file-name>)

If not provided, it will read the file named '<classname>.ini'. It's a
pretty simple and clean interface; it's quite easy to provide default
arguments; it's more readable than a dict-based configuration; and it
works fine, at least for me :-).

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com


More information about the Python-Dev mailing list