Skip to main content

Configuration for humans

Project description

Parsing INI-format configurations with the standard library configparser is painful.

configs provides an easy and clean API for configuration file parsing.

It supports values without section, automatically converts numeric values, automatically handles sections with listed values as lists.

Configurations are easy as they should be in Python!

Installation

pip install configs

Usage

Sample config is fallback.conf:

>>> import configs
>>> conf = configs.load('test/fallback.conf')
>>> print(conf)                         #All config values
{'root': {'path': '../', 'top_level': 'value', 'url': 'http://example.com'}, 'mixed': (['flag'], {'prop': 'val'}), 'list_section': [1, 2.2, 3], 'general': {'spam': 'eggs', 'foo': 'bar'}}
>>> print(conf['root'])                 #Top-level items are stored in the root section
{'path': '../', 'top_level': 'value', 'url': 'http://example.com'}
>>> print(conf['general'])              #Values from the general section
{'foo': 'bar', 'spam': 'eggs'}
>>> print(conf['general']['foo'])       #Value of the foo parameter in the general section
bar
>>> for i in conf['list_section']:      #Sections are iterable
...     print(i * 2)                    #Numeric values are automatically converted to numbers
...
2
4.4
6
>>> for k in conf['general']:           #Key-value sections are also iterable
...     print(k, conf['general'][k])
...
spam eggs
foo bar
>>> mixed_section = conf['mixed']
>>> for m in mixed_section:             #Even mixed sections are iterable: listed values first, key-value next
...     print(m)
...
flag
prop
>>> print(mixed_section.dict_props)     #It is possible to get key-value items separetely...
{'prop': 'val'}
>>> print(mixed_section.list_props)     #...as well as list values
['flag']

New in 1.4: fallback config:

>>> conf_with_fallback = configs.load('test/test.conf', fallback='test/fallback.conf')
>>> print(conf_with_fallback['general'])
baz                                     #The value from ``test.conf`` overrides the value from ``fallback.conf``
>>> print(conf_with_fallback['url'])
http://example.com                      #The value missing in ``test.conf`` is taken from ``fallback.conf``

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
configs-1.7.tar.gz (3.0 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page