Package for creating configuration files automatically and loading objects from those configuration files.
Project description
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.
Project details
Release history Release notifications | RSS feed
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 classconfig-1.0.3.tar.gz.
File metadata
- Download URL: classconfig-1.0.3.tar.gz
- Upload date:
- Size: 22.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dc668253c06bd7c52504ff9352dde440462fc5efad33ddadfc25ddebddeff9e2
|
|
| MD5 |
550ab4611530b4e89867e202b1045323
|
|
| BLAKE2b-256 |
dd9e33140b3f90d3cd2846c5c0afd9834658d042b4272b4ceeb9563a2a47f8a3
|
File details
Details for the file classconfig-1.0.3-py3-none-any.whl.
File metadata
- Download URL: classconfig-1.0.3-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3de0d91eb1c17b63a967a2c4c4447b7ffa2140fbae181aa746a6cd490925e3c
|
|
| MD5 |
a9e4f0eaf9ac44a342c82030b2f020b2
|
|
| BLAKE2b-256 |
518e23cd48cbc705c83c47f8c21caa684d6220dcc2f64a41b26893247f302db6
|