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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kfg-2.0.0.tar.gz | 5.7 kB | Without provenance | Details for kfg-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kfg-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for kfg-2.0.0-py3-none-any.whl |
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
|
Security for kfg 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.