why use special config formats?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Mar 10 20:38:34 EST 2006


On Fri, 10 Mar 2006 06:48:03 -0800, tomerfiliba wrote:

> hey
> 
> i've been seeing lots of config-file-readers for python. be it
> ConfigObj (http://www.voidspace.org.uk/python/configobj.html) or the
> like. seems like a trend to me.
> i came to this conclusion a long time ago: YOU DON'T NEED CONFIG FILES
> FOR PYTHON.

Of course you do.

Sometimes you have to be able to parse and understand existing config
files that have come from somewhere else. If your task is "read and parse
a .ini file", insisting the user re-writes their ini file as Python code
isn't helpful.

Separating code from data is always a good idea. I hope I don't need to
explain why. So you want config files, the only question is, what format
should they be in?

Sometimes it can be useful, especially for quick and dirty apps, to use a
Python module as a config file. But that's not a good idea for production
level code where end users are expected to edit the data: 

# config.py
value = 2.5
colour = "blue"

The user edits value to 3.1, but accidentally puts in a typo "3,1".
Now when your application imports the config.py module, it silently
assigns the tuple (3, 1) to value, and your app dies an unpleasant death
somewhere a long way away. You have no idea why.

So you end up coding defensively to protect against user typos or
stupidity (and believe me, even if your users are technically minded IT
professionals, they will screw up your config files):

# config.py
import logger, sys
value = 2.5  # warning: value must be a float
colour = "blue"  # warning: colour must be one of "red", "blue", "green"
# warning: quotes are compulsory
try:
    colour = colour.strip()
except AttributeError:
    pass
if type(value) != float or value < 0.0:
    logger.log("Value is %s" % value)
    print >>sys.stderr("Bad value, using default")
    value = 2.5
if colour not in ("blue", "red", "green"):
    logger.log("Colour is %s" % value)
    print >>sys.stderr("Bad colour, using default")
    colour = "bleu" # oops, a bug 

and now your config file is code instead of data, and you expect your
users to hack code to change a default value. B--A--D idea.

Using a data config file means you can separate the user-editable data
from the code that verifies that it has sensible values. Your config file
becomes simple again:

# config.py
value = 2.5
colour = "blue"

and your users no longer have to wade through complex code to change a few
defaults, but you still have full flexibility to vet their data.

> why re-invent stuff and parse text by yourself, why the
> interpreter can do it for you? and anyway, i find this a very ugly
> format:
> http://www.voidspace.org.uk/python/configobj.html#the-config-file-format

You are joking right? Pulling our legs?

Here is the config file format, according to the link you supply:

# comment line
# comment line
keyword = value # inline comment

Here is the equivalent written in pure Python:

# comment line
# comment line
keyword = value # inline comment


Why is the first uglier than the second?



-- 
Steven.




More information about the Python-list mailing list