Skip to main content

A configuration library for Python

Project description

Python version Build Status

kfg: A configuration library for Python

kfg provides a Config class which contains your program’s configuration data. It lets you access, manipulate, and validate this data in a straightforward way. It also provides a means to de/serialize the data.

Basic usage

Setting and retrieving configuration values

Getting and setting configuration values is simple:

from kfg.config import Config

# construct a Config
c = Config()

# set the ('ui', 'font-color') value to 'blue'
c['ui', 'font-color'] = 'blue'

# retreive the value
font_color = c['ui', 'font-color']

Uniform exceptions

kfg provides a uniform set of exceptions that are used to signal configuration problems. This means that your program can catch these exceptions and know that they only ever indicate configuration problems. For example:

c = Config()

# kfg throws ConfigKeyError on missing keys.
try:
    x = c['foo']
except ConfigKeyError:
    print('foo not in the config')

Transforms and validation

kfg lets you associate a “transform” with a key. This transform is a 1-arity callable that will be passed the stored value of the configuration option, and kfg will pass the return value of the transform to the user when they access the value. If a transform fails, kfg will raise a ConfigValueError.

This lets you do two things. First, you can construct arbitrary values from stored configuration information in a centralized way, i.e. mediated by the configuration. Second, it let’s you validate configuration values. For example:

c = Config()
c['processing', 'timeout'] = "10 seconds"
c.set_transform(('processing', 'timeout'),
                float)

c['processing', 'timeout']  # Raises ConfigValueError because ``float``
                            # can't parse "10 seconds"

This system is intentionally low-powered and simple. For example, kfg allows you to set values which violate the transform. The goal is not to create completely water-tight configurations, but rather to create systems which are easy to get right and which warn you when you might be using an invalid value.

Rationale

There’s no real rocket science in kfg, and you can get most of its features just by using dictionaries, lists, tuples, etc. There are a few problems with using “raw” data structures like that for configuration, though.

First, you’ll get standard exceptions like KeyError and IndexError when you try to access missing values. Since these kinds of errors can come from almost anywhere in a system, it’s not easy to differentiate between those that come from configuration problems and the others. By providing specialized “configuration” errors, you can catch kfg exceptions and be confident that they point to configuration errors.

Second, kfg lets you centralize the basic configuration validation/transformations. Configuration values may be used in many places in a system, so it’s often helpful to have a single point of validation for them.

Project details


Download files

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

Source Distribution

kfg-2.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

kfg-2.0.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file kfg-2.0.0.tar.gz.

File metadata

  • Download URL: kfg-2.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for kfg-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0b0d8b76bd9cf3e970949fde7241361f31a7551c9355725f6fc3dc15834ed915
MD5 385fb0281a363732744c34e04ab1b5ee
BLAKE2b-256 23f47005c34e0f21c81b3192b4fba46b09953020f1e603e8c3d32a3a3b0b449c

See more details on using hashes here.

File details

Details for the file kfg-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: kfg-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for kfg-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2fd1dc81ba2740dc2567b41f984f3e3d4e6bccde5d4096e52e147aade4b1d7e4
MD5 3905b7b2f94553a2f43ed7151b10b1f9
BLAKE2b-256 99421be913146ef9900346c44814982f29cd297e2e180295c8ad1639182a508b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page