On Sun, 17 Oct 2004 15:10:44 -0700, Michael Chermside email@example.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 :-).