A a simple python configuration management and object instantiation tool
Project description
konfigurator
konfigurator is a lightweight Python configuration management utility that allows you to define, override, and instantiate configuration dictionaries and Python classes from simple config files or command-line arguments.
Further, it allows you to instantiate objects from classes using import paths. In this way, no global registry or any disclosure of your private code is needed.
The instantiation via import path is inspired by https://github.com/Farama-Foundation/HighwayEnv
Features
- Load Python-based configuration files as dictionaries.
- Override configuration parameters via command-line.
- Instantiate Python classes from config dictionaries using import paths.
- Save resolved configs to JSON and reload them later.
Installation
cd into repository and run
pip install .
To install in developer mode (with pre-commit and pytest) run
pip install -e .[dev]
Usage
-
Load a configuration file
Your configuration file is a pure python file (e.g., config.py) should define a dictionary named config:
# config.py work_dir = "/tmp/my_work_dir" class_config_1: { "type": "my_module.MyClass", "name": "default_name" }
You will be able to instantiate an object from the configuration
class_config_1(see ).Load this configuration in Python:
from konfigurator import load_config config = load_config(config_path="config.py")
-
Override from command-line and save result
You can override nested config values via CLI and save the modified config to disk, e.g.:
python script.py \ --config config.py \ --override experiment_dir=/tmp/experiment \ --override class_config_1.name=overridden_name \ --override class_config_1.type=5.0
Currently, floats, ints, and booleans are converted into their respective type. Strings and other types remain strings.
-
Instantiate classes from config
Use the
instantiate_class_from_configto build objects dynamically (IMPORTANT: the value for the keytypedefines the import path):from konfigurator import instantiate_object_from_config class_config_1: { "type": "my_module.MyClass", "name": "default_name" } my_obj = instantiate_object_from_config(class_config_1)
-
Save and reload a resolved config as JSON
from konfigurator import load_config, save_config_to_json, load_config_from_json config = load_config(config_path="config.py") save_config_to_json(config, "config.json") # later, e.g. in a different process config = load_config_from_json("config.json")
Deployment
Releases are built and published to PyPI manually using build
and twine, both included in the dev extra.
-
Bump the
versionfield inpyproject.toml(follow SemVer). -
Build the source distribution and wheel:
python -m build
This produces
dist/konfigurator-<version>.tar.gzanddist/konfigurator-<version>-py3-none-any.whl. -
Sanity-check the built artifacts before uploading:
twine check dist/*
-
(Optional but recommended) Upload to TestPyPI first and verify the install works:
twine upload --repository testpypi dist/* pip install --index-url https://test.pypi.org/simple/ konfigurator==<version>
-
Upload to PyPI:
twine upload dist/*
twinewill prompt for PyPI credentials, or read them from~/.pypirc/ theTWINE_USERNAMEandTWINE_PASSWORD(orTWINE_API_KEY) environment variables. Using a scoped PyPI API token as the password (with username__token__) is recommended over a personal password. -
Tag the release in git and push the tag, e.g.:
git tag v<version> git push origin v<version>
There is currently no automated release workflow — publishing is a manual, local step.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file konfigurator-0.3.0.tar.gz.
File metadata
- Download URL: konfigurator-0.3.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b997ee6c8076ce4cfc27a0804c0944758eba76ee150b8d780b2c13773f4dbaa4
|
|
| MD5 |
5b0cdede04f09934e6e949e79e23cd47
|
|
| BLAKE2b-256 |
45ac110e6f7e00f70fff4e4b11faa9a2e0bcf00cc1d4373f5ba87d132436e602
|
File details
Details for the file konfigurator-0.3.0-py3-none-any.whl.
File metadata
- Download URL: konfigurator-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
460a0b76fdc79a1dd1c10ebda20acbbb64ec0ed91ca2e7b153233f504f000eb5
|
|
| MD5 |
3e1580fbc7f959d056f0a0fbc52deec5
|
|
| BLAKE2b-256 |
ed7193c733d537c9b01c20a1ac53ae053a99962e4706f8ccab349e2367867874
|