Oh, ConfigParser, how I wish you would go away. You're un-pythonic and strict to the point of senslesness. You're parsing simple key/value files yet you won't give me a dictionary -- what are you smoking? And why are you in the standard library?
Finally fed up with yet another ConfigParser debacle (hint: try reading a git config file with ConfigParser), I've written my own replacement. I've called it configdict, and you find it on GitHub. configdict has the following properties:
- When you parse an INI style config file, you get back a dictionary of dictionaries.
- Dictionaries can "inherit" keys from the DEFAULT section.
Also, for the record:
- It's largely untested (other than in my little personal projects), and might eat your cat.
This means that given a config file:
[DEFAULT] in stock = yes [widgets] price = 110.00 size = large [sprockets] price = 15.99 shape = oblong
You can do this:
>>> config = configdict.ConfigDict('myconfig.ini')
>>> print config['widgets']['price']
110.00
>>> print config['widgets']['in stock']
yes
And look, it really is a dictionary:
>>> import ppprint
>>> pprint.pprint(config)
{'DEFAULT': {'in stock': 'yes'},
'__GLOBAL__': {},
'widgets': {'description': '%(size)s widgets',
'options': 'this that and the other thing',
'price': '110.00',
'size': 'large'}}
It supports some reasonably common features of this sort of configuration file. For example, line continuation:
options = this \ that \ and \ the other thing
Which yields:
>>> c['sprockets']['options'] 'this that and the other thing'
0 comments:
Post a Comment