2 Example

Load objects from a textual representation, stored in snippet:

>>> import miniconf
>>> snippet = 'spam = [ 1, True, ("test", None) ]; egg = -2'
>>> config = miniconf.load(snippet)
>>> print config
{'egg': -2, 'spam': [1, True, ('test', None)]}

Of course, config could have been constructed from snippet as well by doing:

>>> config = {}
>>> exec snippet in config
>>> del config['__builtins__']

The whole point of using miniconf instead of the exec statement is that it is safer, since no arbitrary code execution ever occurs: the code is only parsed, not executed, and the objects are reconstructed from the snippet abstract syntax tree. In practice, it makes user access to simple pythonic data structure possible without having to fear injection of unwanted third-party code.

Finally, let's modify config a little and dump it back:

>>> config['egg'] = u'new_value'
>>> config['new'] = range(10)
>>> print miniconf.dump(config)
egg = u'new_value'

new = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

spam = [1, True, ('test', None)]