A compositional configuration library for Python
Project description
CompoConf
CompoConf is a Python library for compositional configuration management. It provides a type-safe way to define, parse, and instantiate configurations for complex, modular systems.
Features
- Type-safe configuration parsing with dataclass support
- Registry-based class instantiation
- Inheritance-based interface registration
- Support for nested configurations
- Optional OmegaConf integration
- Strict type checking and validation
Installation
pip install compoconf
Quick Start
Here's a simple example of how to use CompoConf:
from dataclasses import dataclass
from compoconf import (
RegistrableConfigInterface,
ConfigInterface,
register_interface,
register,
)
# Define an interface
@register_interface
class ModelInterface(RegistrableConfigInterface):
pass
# Define a configuration
@dataclass
class MLPConfig(ConfigInterface):
hidden_size: int = 128
num_layers: int = 2
# Register a class with its configuration
@register
class MLPModel(ModelInterface):
config_class = MLPConfig
def __init__(self, config):
self.config = config
# Initialize model with config...
# Create and use configurations
config = MLPConfig(hidden_size=256)
model = config.instantiate(ModelInterface)
Advanced Usage
Nested Configurations
CompoConf supports nested configurations through type annotations:
@dataclass
class TrainerConfig(ConfigInterface):
model: ModelInterface.cfgtype # References the interface type
learning_rate: float = 0.001
# Parse nested configuration
config = {
"model": {
"class_name": "MLPModel",
"hidden_size": 256
},
"learning_rate": 0.01
}
trainer_config = parse_config(TrainerConfig, config)
Type Safety
The library provides comprehensive type checking:
- Validates configuration values against their type annotations
- Ensures registered classes match their interfaces
- Checks for missing required fields
- Supports strict mode for catching unknown configuration keys
OmegaConf Integration
CompoConf optionally integrates with OmegaConf for enhanced configuration handling:
from omegaconf import OmegaConf
# Load configuration from YAML
conf = OmegaConf.load('config.yaml')
config = parse_config(ModelConfig, conf)
Registry System
The registry system allows for dynamic class instantiation based on configuration:
# Register multiple implementations
@dataclass
class CNNConfig(ConfigInterface):
kernel_size: int = 4
@register
class CNNModel(ModelInterface):
config_class = CNNConfig
@dataclass
class TransformerConfig(ConfigInterface):
hidden_size: int = 128
num_heads: int = 4
@register
class TransformerModel(ModelInterface):
config_class = TransformerConfig
# Configuration automatically creates correct instance
config = {
"model": {
"class_name": "TransformerModel",
"num_heads": 8,
"hidden_size": 512
}
}
API Reference
Core Classes
RegistrableConfigInterface: Base class for interfaces that can be configuredConfigInterface: Base class for configuration dataclassesRegistry: Singleton managing registration of interfaces and implementations
Decorators
@register_interface: Register a new interface@register: Register an implementation class
Functions
parse_config(config_class, data, strict=True): Parse configuration data into typed objects
Enhanced Functionality
Parsing Module
The parsing module has been enhanced to provide more robust and flexible configuration parsing capabilities. Key improvements include:
- Improved handling of nested configurations and unions.
- Enhanced type validation and error reporting.
- Support for parsing configurations from various data sources (e.g., JSON, YAML).
Util Module
The util module now includes powerful utilities for dynamic configuration and validation:
partial_call: Enables the creation of configurable classes from functions, allowing for dynamic modification of function arguments through configuration.from_annotations: Simplifies the creation of configurable classes by automatically extracting configuration parameters from class annotations.validate_literal_fieldandassert_check_literals: Provide mechanisms for validating Literal type annotations in dataclasses, ensuring that configuration values are within the allowed set of options.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License
Author
Korbinian Pöppel (korbip@korbip.de)
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 compoconf-0.1.1.tar.gz.
File metadata
- Download URL: compoconf-0.1.1.tar.gz
- Upload date:
- Size: 30.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08513232711df8742d6ac67b1549fb21f0bd20da39f38e7e59d038ed5e3ef4f8
|
|
| MD5 |
9b70086540f09d7e84df4f3b87e7e945
|
|
| BLAKE2b-256 |
eabbb3f2918aee939081b03a62a83566d19ba60f9d2505d3f17968aa638646f2
|
Provenance
The following attestation bundles were made for compoconf-0.1.1.tar.gz:
Publisher:
publish.yaml on kpoeppel/compoconf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
compoconf-0.1.1.tar.gz -
Subject digest:
08513232711df8742d6ac67b1549fb21f0bd20da39f38e7e59d038ed5e3ef4f8 - Sigstore transparency entry: 417409052
- Sigstore integration time:
-
Permalink:
kpoeppel/compoconf@1fe798eee5e265c19a4a6fe129058d9d2a47393c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/kpoeppel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@1fe798eee5e265c19a4a6fe129058d9d2a47393c -
Trigger Event:
release
-
Statement type:
File details
Details for the file compoconf-0.1.1-py3-none-any.whl.
File metadata
- Download URL: compoconf-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d60c689255c536f4901f1fea822ce4b28c8f97537fd98097282d6d465f3973cd
|
|
| MD5 |
ebfae6f9febff976727426e3760ef33e
|
|
| BLAKE2b-256 |
d235d6ecef2bc382f1ea4478fde2d508659529c8aa7d35bbc0794fac8a9c1e6d
|
Provenance
The following attestation bundles were made for compoconf-0.1.1-py3-none-any.whl:
Publisher:
publish.yaml on kpoeppel/compoconf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
compoconf-0.1.1-py3-none-any.whl -
Subject digest:
d60c689255c536f4901f1fea822ce4b28c8f97537fd98097282d6d465f3973cd - Sigstore transparency entry: 417409059
- Sigstore integration time:
-
Permalink:
kpoeppel/compoconf@1fe798eee5e265c19a4a6fe129058d9d2a47393c -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/kpoeppel
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@1fe798eee5e265c19a4a6fe129058d9d2a47393c -
Trigger Event:
release
-
Statement type: