Skip to main content

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.

Metadata

Release files for kfg 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kfg 2.0.0
File Size Uploaded
kfg-2.0.0.tar.gz 5.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kfg 2.0.0
File Interpreter ABI Platform
kfg-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.2 kB

Release files / kfg-2.0.0.tar.gz

Download URL kfg-2.0.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0b0d8b76bd9cf3e970949fde7241361f31a7551c9355725f6fc3dc15834ed915
BLAKE2b-256 checksum
How to use checksums
23f47005c34e0f21c81b3192b4fba46b09953020f1e603e8c3d32a3a3b0b449c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / kfg-2.0.0-py3-none-any.whl

Download URL kfg-2.0.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2fd1dc81ba2740dc2567b41f984f3e3d4e6bccde5d4096e52e147aade4b1d7e4
BLAKE2b-256 checksum
How to use checksums
99421be913146ef9900346c44814982f29cd297e2e180295c8ad1639182a508b
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.1.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page