classconfig
Package for creating (yaml) configuration files automatically and loading objects from those configuration files.
Installation
You can install it using pip:
pip install classconfig
Usage
At first we need a class that is configurable it means that it has ConfigurableAttribute class members. Such as
ConfigurableValue or ConfigurableFactory. Let's create two simple classes where one of them will have the other
instance as a member:
from classconfig import ConfigurableValue, ConfigurableFactory, ConfigurableMixin
class Inventory(ConfigurableMixin):
size: int = ConfigurableValue(desc="Size of an inventory", user_default=10, validator=lambda x: x > 0)
class Character(ConfigurableMixin):
lvl: int = ConfigurableValue(desc="Level of a character", user_default=1, validator=lambda x: x > 0)
name: str = ConfigurableValue(desc="Name of a character")
inventory: Inventory = ConfigurableFactory(desc="Character's inventory", cls_type=Inventory)
You can see that the usage is similar to dataclasses as it also uses descriptors. You can omit the ConfigurableMixin
inheritance but then you will have to write your own __init__ method e.g.:
class Inventory:
size: int = ConfigurableValue(desc="Size of an inventory", user_default=10, validator=lambda x: x > 0)
def __init__(self, size: int):
self.size = size
Creating configuration file
Now let's create a configuration file for our Character class. You can do it by calling save method of Config class:
from classconfig import Config
Config(Character).save("config.yaml")
You will get a file with the following content:
lvl: 1 # Level of a character
name: # Name of a character
inventory: # Character's inventory
size: 10 # Size of an inventory
Validation
As you have seen in the previous example, it is possible to add a validator.
A validator could be any callable that takes one argument and return True when valid.
You can also raise an exception if the argument is invalid to specify the reason for the failure.
There are various predefined validators in classconfig.validators module.
Transformation
It is possible to specify a transformation (transform attribute) that will transform user input. The transformation
is done before the validation. Thus, it can be used to transform input into valid form.
It can be any callable that takes one argument and returns the transformed value, but there also exist some predefined
transformations in classconfig.transforms module.
Loading
Now let's load the configuration file we just created and create an instance of Character class:
from classconfig import Config, ConfigurableFactory
config = Config(Character).load(path) # load configuration file
loaded_obj = ConfigurableFactory(Character).create(config) # create an instance of Character class
Why YAML?
YAML is a human-readable data serialization language. It is easy to read and write. It is also easy to parse and generate.
It supports hierarchical data structures, which are very useful when you need to represent configuration of a class that has other configurable classes as members.
It supports comments, unlike e.g. json, which is also a big advantage.
Metadata
Release files for classconfig 1.0.18
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 |
|---|---|---|---|---|
| classconfig-1.0.18.tar.gz | 26.4 kB | Without provenance | Details for classconfig-1.0.18.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| classconfig-1.0.18-py3-none-any.whl | Python 3 | none | any | Details for classconfig-1.0.18-py3-none-any.whl |
Total release size: 46.2 kB
Release files / classconfig-1.0.18.tar.gz
| Download URL | classconfig-1.0.18.tar.gz |
|---|---|
| Size | 26.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
410f12f5b780264294d4137740eb838ea1ce6fb84b6167d7d2677d53748986b6
|
|
BLAKE2b-256 checksum How to use checksums |
4ad1aac5943148888844cfb270bdb9473ab5ece79649bdb4a6727f4762051979
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Release files / classconfig-1.0.18-py3-none-any.whl
| Download URL | classconfig-1.0.18-py3-none-any.whl |
|---|---|
| Size | 19.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a952f4c0955daacbbfec5f7a662f323236f5bb43aa66c80ad8ce2a6f4b676ce1
|
|
BLAKE2b-256 checksum How to use checksums |
aa1a92f693c318dc2a86d76e6f3d119ebf9c6573246e6b17af60c861a4234c12
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.25
|
Security for classconfig 1.0.18
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.