V
Vinay Sajip
A new version of the Python config module has been released.
What Does It Do?
================
The config module allows you to implement a hierarchical configuration
scheme with support for mappings and sequences, cross-references
between one part of the configuration and another, the ability to
flexibly access real Python objects, facilities for configurations to
include and cross-reference one another, simple expression evaluation
and the ability to change, save, cascade and merge configurations. You
can easily integrate with command line options using optparse.
This module has been developed on python 2.3 but should work on
version 2.2 or greater. A test suite using unittest is included in the
distribution.
A very simple configuration file (simple.cfg):
# starts here
message: Hello, world!
#ends here
a very simple program to use it:
from config import Config
cfg = Config(file('simple.cfg'))
print cfg.message
results in:
Hello, world!
Configuration files are key-value pairs, but the values can be
containers that contain further values.
A simple example - with the example configuration file:
messages:
[
{
stream : `sys.stderr`
message: 'Welcome'
name: 'Harry'
}
{
stream : `sys.stdout`
message: 'Welkom'
name: 'Ruud'
}
{
stream : $messages[0].stream
message: 'Bienvenue'
name: Yves
}
]
a program to read the configuration would be:
from config import Config
f = file('simple.cfg')
cfg = Config(f)
for m in cfg.messages:
s = '%s, %s' % (m.message, m.name)
try:
print >> m.stream, s
except IOError, e:
print e
which, when run, would yield the console output:
Welcome, Harry
Welkom, Ruud
Bienvenue, Yves
The above example just scratches the surface. There's more information
about this module available at
http://www.red-dove.com/python_config.html
Comprehensive API documentation is available at
http://www.red-dove.com/config/index.html
As always, your feedback is most welcome (especially bug reports,
patches and suggestions for improvement). Enjoy!
Cheers
Vinay Sajip
Red Dove Consultants Ltd.
Changes since the last release posted on comp.lang.python:
==========================================================
Made classes derive from object (previously they were old-style
classes).
Changed ConfigMerger to use a more flexible merge strategy.
Multiline strings (using """ or ''') are now supported.
A typo involving raising a ConfigError was fixed.
Added ConfigOutputStream to provide better Unicode output support.
Altered save code to put platform-dependent newlines for Unicode.
What Does It Do?
================
The config module allows you to implement a hierarchical configuration
scheme with support for mappings and sequences, cross-references
between one part of the configuration and another, the ability to
flexibly access real Python objects, facilities for configurations to
include and cross-reference one another, simple expression evaluation
and the ability to change, save, cascade and merge configurations. You
can easily integrate with command line options using optparse.
This module has been developed on python 2.3 but should work on
version 2.2 or greater. A test suite using unittest is included in the
distribution.
A very simple configuration file (simple.cfg):
# starts here
message: Hello, world!
#ends here
a very simple program to use it:
from config import Config
cfg = Config(file('simple.cfg'))
print cfg.message
results in:
Hello, world!
Configuration files are key-value pairs, but the values can be
containers that contain further values.
A simple example - with the example configuration file:
messages:
[
{
stream : `sys.stderr`
message: 'Welcome'
name: 'Harry'
}
{
stream : `sys.stdout`
message: 'Welkom'
name: 'Ruud'
}
{
stream : $messages[0].stream
message: 'Bienvenue'
name: Yves
}
]
a program to read the configuration would be:
from config import Config
f = file('simple.cfg')
cfg = Config(f)
for m in cfg.messages:
s = '%s, %s' % (m.message, m.name)
try:
print >> m.stream, s
except IOError, e:
print e
which, when run, would yield the console output:
Welcome, Harry
Welkom, Ruud
Bienvenue, Yves
The above example just scratches the surface. There's more information
about this module available at
http://www.red-dove.com/python_config.html
Comprehensive API documentation is available at
http://www.red-dove.com/config/index.html
As always, your feedback is most welcome (especially bug reports,
patches and suggestions for improvement). Enjoy!
Cheers
Vinay Sajip
Red Dove Consultants Ltd.
Changes since the last release posted on comp.lang.python:
==========================================================
Made classes derive from object (previously they were old-style
classes).
Changed ConfigMerger to use a more flexible merge strategy.
Multiline strings (using """ or ''') are now supported.
A typo involving raising a ConfigError was fixed.
Added ConfigOutputStream to provide better Unicode output support.
Altered save code to put platform-dependent newlines for Unicode.