Skip to main content

Sensible way to grab config from various sources

Project description

So you have some configuration variables, and you want them to be available to be in any number of ini-like files, as well as overridable from the environment, and overridable from the command line. Define once, and use.

>>> options = [
...    Option('debug', 'Run in debug mode', False,
...           short_name='d', converter=bool, action='store_true'),
... ]
>>> conf = Config(options)
>>> conf.debug # Will start as the default value
False

This time we shall pass an env prefix to look up on, so as not to pollute any environment namespace too badly:

>>> conf = Config(options, 'PONY')
>>> conf.grab_from_env({'PONY_DEBUG': '1'})
>>> conf.debug
True

Note

The optional env dict parameter will default to os.environ if it is omitted.

Now we can grab some stuff from argv:

>>> conf = Config(options)
>>> conf.grab_from_argv(['--debug'])
[]
>>> conf.debug
True

Also, remember that you can serialize these things:

>>> conf = Config(options)
>>> conf.to_dict()
{'debug': False}

Note

In real life you would import this:

from sensibleconfig import Config, Option

So as you can see above, the options were declared, and then the config object was created from those options. It is imagined that an application may collate the options from many different places, such as plugins which wish to define their own options.

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
sensibleconfig-0.5.tar.gz (7.1 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