Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

classconfig-1.0.18.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classconfig-1.0.18-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file classconfig-1.0.18.tar.gz.

File metadata

  • Download URL: classconfig-1.0.18.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for classconfig-1.0.18.tar.gz
Algorithm Hash digest
SHA256 410f12f5b780264294d4137740eb838ea1ce6fb84b6167d7d2677d53748986b6
MD5 458e379962f7037ec271070f8fd11a3f
BLAKE2b-256 4ad1aac5943148888844cfb270bdb9473ab5ece79649bdb4a6727f4762051979

See more details on using hashes here.

File details

Details for the file classconfig-1.0.18-py3-none-any.whl.

File metadata

  • Download URL: classconfig-1.0.18-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for classconfig-1.0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 a952f4c0955daacbbfec5f7a662f323236f5bb43aa66c80ad8ce2a6f4b676ce1
MD5 449142694a49f976392bd5054772a2df
BLAKE2b-256 aa1a92f693c318dc2a86d76e6f3d119ebf9c6573246e6b17af60c861a4234c12

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page