Python package to assist configuration
Project description
yaconfig
Python package to assist configuration of Python software.
Installation
To install the latest release:
pip3 install yaconfig
Usage
This package is intended to be used by other package to assist their configuration. The recommended workflow follows:
- Add a config module. Its purpose should be to define the configuration variables (the metaconfig) and to provide a single instance of the actual configuration (the values) for your application. If you want to implement a more complex logic to handle multiple configuration, you can do it here. Simple example:
"""File config.py"""
import yaconfig
metaconfig = yaconfig.MetaConfig(
yaconfig.Variable("text", type=str, default="Hello world", help="Text to output")
)
# Get a default configuration, which should be overridden when the execution starts
config = yaconfig.Config(metaconfig)
- In the entry point of your program, load the previous module and initialize the config using the desired method. The variables can be accessed from now on. Example:
"""File main.py"""
from config import config # Just need the config object of the previous module
# Use a relative import instead if intended to run as a package
# from .config import config
try:
config.load_json("config.json")
except FileNotFoundError:
print("config.json file not found. Using the default configuration instead.")
print(config["text"])
Note that if that file can be loaded as a module, you should avoid initializing the config. You can use __main__
or
build a launching script to prevent this.
- To document the configuration, you can use the methods of the metaconfig variable you have defined. This can be done manually from the interpreter or automated by writing a script. Some examples follow:
from config import metaconfig # Or wherever the config was placed
metaconfig.generate_json_example() # Generate a config.example.json file
metaconfig.generate_environment_example() # Generate a environment.example.sh file
metaconfig.interactive_json() # Prompt the user to generate a config.json file
metaconfig.interactive_environment() # Prompt the user to generate a environment.sh file
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
yaconfig-0.4.1.tar.gz
(6.3 kB
view details)
Built Distribution
File details
Details for the file yaconfig-0.4.1.tar.gz
.
File metadata
- Download URL: yaconfig-0.4.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c89b2495f286b896638d115bb8898bda3a849880e6eda614a55b84b82a52aded |
|
MD5 | 8422717cbf9f8b8b382d2d6154c1da6b |
|
BLAKE2b-256 | e799805281e6b8cb328282a17da73cb23723bda0c789f5072a207cae9301132e |
File details
Details for the file yaconfig-0.4.1-py2.py3-none-any.whl
.
File metadata
- Download URL: yaconfig-0.4.1-py2.py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 406a5741fbfb02fe387d38cd1d90a7d4b75bb6d10768f69e6022e7631ba98cbc |
|
MD5 | 8890731b6c5f02ed88c62fc23ed33c72 |
|
BLAKE2b-256 | 7fcbb5417b9d9345faf3ca444ad3acb7d3604cf2deee9be87dfa5b661a4e0357 |