Python config module 0.3.6 review

Download
by rbytes.net on

Python config module allows a hierarchical configuration scheme with support for mappings and sequences, cross-references between one

License: Freely Distributable
File size: 30K
Developer: Vinay Sajip
0 stars award from rbytes.net

Python config module allows 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 without full-blown eval(), an include facility, simple expression evaluation, and the ability to change, save, cascade, and merge configurations.

It interfaces easily with environment variables and command line options. Python config module has been developed on Python 2.3, but should work on version 2.2 or greater.

Usage

The simplest scenario is, of course, "Hello, world". Let's look at a very simple configuration file simple.cfg where a message to be printed is configured:

# The message to print (this is a comment)
message: 'Hello, world!'

and the program which uses it:

from config import Config

# You can pass any file-like object; if it has a name attribute,
# that name is used when file format error messages are printed
f = file('simple.cfg')
cfg = Config(f)
print cfg.message

which results in the expected:

Hello, world!

A configuration file is, at the top level, a list of key-value pairs. Each value, as we'll see later, can be a sequence or a mapping, and these can be nested without any practical limit.

In addition to attribute access (cfg.message in the example above), you can also access a value in the configuration using the getByPath method of a configuration: cfg.getByPath('message') would be equivalent. The parameter passed to getByPath is the path of the required value. The getByPath method is useful for when the path is variable. It could even be read from a configuration.

There is also a get method which acts like the dictionary method of the same name - you can pass a default value which is returned if the value is not found in the configuration. The get method works with dictionary keys or attribute names, rather than paths. Hence, you may call cfg.getByPath('a.b') which is equivalent to cfg.a.b, or you can call cfg.a.get('b', 1234) which will return cfg.a.b if it is defined, and 1234 otherwise.

What's New in This Release:
This release makes classes derive from an object (previously, they were old-style classes).
ConfigMerger has been changed to use a more flexible merge strategy.
Multi-line strings (using """ or ''') are now supported.
A typo involving raising a ConfigError was fixed.

Python config module 0.3.6 search tags