Friday, October 9, 2009

Python Configuration Manipulation

Configuration management is an important aspect of any application. Configuration is necessary unless the application in question is a monolithic, inflexible piece of code. And, there are applications such as these but they aren't the norm. Many aspects of most applications will need to change, even if only subtly. The users running these applications need an easy way to manage and manipulate these configuration values.

The INI configuration style is probably the best standard to follow. It is easy for both users and applications to understand and to manipulate. Moreover, there exist libraries for handling these files in almost any programming language.

In Python, the ConfigObj package is good at dealing with configuration files. It is good at both reading and manipulating the files which is useful because developers need not depend on one package for reading configuration files and another for writing them. There is a single class that Python applications should be concerned with in the ConfigObj package. The configuration objects are instantiated with the file name containing the configuration.

Perhaps the best feature of ConfigObj instances is that configuration values can be read and written using dictionary style notation. This is much more intuitive for Python developers than using some kind of configuration specific notation.

However, since the configuration values are accessed using dictionary notation, it would be trivially easy to abstract away the configuration values as attributes of some domain specific class. The same configuration file may in fact be read by various domain classes. Each class reading a different section of the configuration.