Skip to main content

A simple wrapper around dataclasses, for general configuration

Project description

Confclasses

Create dataclass-style classes that can be used for configuring a Python tool. The idea is to handle loading and saving the config from files while allowing simpler IDE usage with the config. We don't need 90% of the features in dataclasses, so we make the API easier.

The module is designed to have a global config object that is instantiated at the start and then loaded dynamically later. Please see the common usage section for an example.

confclasses_comments is also shipped with this tool. It uses ruamel.yaml to add comments and ast to get the "docstring" of the annotations (fields) in the config classes.

Common usage

Create a config.py to store the config.

# config.py
@confclass
class RepeatingConfig:
    test: str
    default1: int = 123
    
@confclass
class NestedConfig:
    field1: str = "foo"
    field2: str = "bar"
    """ test document for field 2 """
    hashed_field3: RepeatingConfig = RepeatingConfig(test="nested")

@confclass
class ExampleConfig:
    nested: NestedConfig
    field3: int = 42
    """ test document for field 3 """
    hashed_field1: list = ["test", "items"]
    hashed_field2: dict = {"key1": "value1"}
    hashed_field4: RepeatingConfig = RepeatingConfig(test="base")

config = ExampleConfig()

Loading it at the start

# main.py
from confclasses import load_config
from config import config
from .example_module import example_function

def main():
    with open('conf.yaml', 'r') as f:
        load_config(config, f.read())
    
    example_function()

In the example_module

# example_module.py
from config import config

def example_function():
    print(config.field3)

Planned changes

Feature Description Status
XDG support Automatic detection of config files using XDG Planned
Sort out comments Either move confclasses_comments to its own repo or document clearer how it works Open to discussion
Standardize save_config confclasses and confclasses_comments have different arguments, it can be better Planned

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

confclasses-0.0.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

confclasses-0.0.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file confclasses-0.0.1.tar.gz.

File metadata

  • Download URL: confclasses-0.0.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for confclasses-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e949fa61e51a2c929f686102b2ca25b50f60184f7c1d332d0a7fac17f6575473
MD5 61f454337d282ec996fb06ff9e80c2c4
BLAKE2b-256 cd481564c2fcb7ed29da8f55124cf504a2aacde341efe8cca9470b0f3abf980c

See more details on using hashes here.

File details

Details for the file confclasses-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: confclasses-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for confclasses-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a233234e3d8af79937e838fd49d6701f9d1f24c958b33fde0e3f2c6f6ba23094
MD5 f083d92dbd70c8ad56cc5c458e6f7872
BLAKE2b-256 8694c6fe935d3c2c3d89122ee12c85c0c365fef420aeaa3a120455cd86a35a4e

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