Skip to main content

This module provides classes and utilities for managing configurations, validating schemas, and creating customizable objects from configuration data. It is particularly useful for AI applications where configurations can be complex and need to be validated at runtime.

Project description

Configurable Components Library

Installation

To install this package, you can use Conda with the included development tools:

conda env create -f environment.yml

or

pip install customizable

Usage

Key Concepts

This package allows adding or modifying any component in a modular way thanks to the architecture based on Customizable and configuration schemas. All components (models, datasets, optimizers, metrics, etc.) follow this principle.


Modular Architecture with Customizable and TypedCustomizable

This library relies on a modular architecture through the base classes Customizable and TypedCustomizable. These classes provide flexible, extensible, and standardized configuration of components (models, datasets, optimizers, etc.).

1. Customizable: Dynamic Component Creation

Customizable is a base class that uses schemas (Schema) to dynamically validate configurations. It enables:

  • Validation: Each parameter is validated by type and constraint before instantiation using the Schema class.
  • Flexibility: Loading configurations from Python dictionaries or YAML files. The configurations are dynamic since the parameters depend on the requested object/class type.
  • Automatic attribute assignment: Configuration parameters are automatically set as instance attributes, removing the need to manually assign them in the __init__ method.
  • Automatic precondition checks: The preconditions() method is automatically called, ensuring validation before instantiation.

Example:

from configs.config import Customizable, Schema

class MyComponent(Customizable):
  config_schema = {
    'learning_rate': Schema(float, default=0.01),
    'batch_size': Schema(int, default=32),
  }

  def preconditions(self):
      assert self.learning_rate > 0, "Learning rate must be positive"

  def __init__(self):
      pass

2. TypedCustomizable: Dynamic Subclass Management with Abstraction

TypedCustomizable extends Customizable by adding the ability to dynamically select a subclass to instantiate based on a type parameter.

To ensure proper implementation, abstract base classes (ABC) can be used to enforce method definitions in subclasses.

Example: Using TypedCustomizable for Automatic Component Selection with Abstract Methods

from configs.config import TypedCustomizable, Schema
import abc

class BaseComponent(TypedCustomizable, abc.ABC):
  aliases = ['base_component']

  @abc.abstractmethod
  def process(self):
      """This method must be implemented in subclasses"""
      pass

class SpecificComponentA(BaseComponent):
  aliases = ['component_a']
  config_schema = {
    'param1': Schema(int, default=10),
  }

  def process(self):
      return f"Processing with param1: {self.param1}"

class SpecificComponentB(BaseComponent):
  aliases = ['component_b']
  config_schema = {
    'param2': Schema(str, default="default_value"),
  }

  def process(self):
      return f"Processing with param2: {self.param2}"

config_a = {'type': 'component_a', 'param1': 20}
component_a = BaseComponent.from_config(config_a)
print(component_a.process())  # Processing with param1: 20

config_b = {'type': 'component_b', 'param2': "custom_value"}
component_b = BaseComponent.from_config(config_b)
print(component_b.process())  # Processing with param2: custom_value

Why Use This Library?

By leveraging Customizable and TypedCustomizable, this library allows:

  • Modular and scalable design: New components can be added with minimal modifications.
  • Configuration-driven instantiation: Easily switch between different implementations using YAML or JSON configurations.
  • Strong type and schema validation: Ensures correct parameters and prevents misconfigurations.
  • Abstract base classes for contract enforcement: Guarantees that all subclasses implement required methods.
  • Preconditions to validate component state: Ensures that instantiated components are correctly configured without requiring manual calls.

Schema Functionality

Schema Class Concept

The Schema class defines the expected structure for each configuration parameter. It plays a central role in validation and default value application during object instantiation.

Main attributes of Schema:

  • type: Specifies the expected type (e.g., int, float, str).
  • default: Defines a default value if the parameter is not provided.
  • optional: Indicates whether the parameter is optional.
  • aliases: Allows using alternative names for the same parameter.

A predefined Config type is also provided for flexibility:

from typing import Union
Config = Union[dict, str]

This allows configuration data to be passed as either a dictionary or a YAML file path.

Adding a Component in Practice

Customizable

Define the class: Inherit from the appropriate base class (e.g., BaseComponent) or directly from Customizable and implement the required logic.

class NewComponent(Customizable):
  config_schema = {
    'param1': Schema(str),
    'param2': Schema(int, default=10),
  }

  def preconditions(self):
      assert self.param2 >= 0, "param2 must be non-negative"

Using configuration-based instantiation:

component:
  param1: "example"
import NewComponent

component = NewComponent.from_config(config['component'])

With TypedCustomizable, dynamically selecting the right implementation is straightforward, making this approach ideal for large-scale, evolving systems.

Contact: julienrabault@icloud.com

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

customizable-0.1.3.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

customizable-0.1.3-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file customizable-0.1.3.tar.gz.

File metadata

  • Download URL: customizable-0.1.3.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.9

File hashes

Hashes for customizable-0.1.3.tar.gz
Algorithm Hash digest
SHA256 88fa389e7d651258f926f3731b66f5d6d932530ab6101cb6a95867b9f6495311
MD5 822146a1311906e6aae39a9ce8a1feff
BLAKE2b-256 1fc92f1fd2784942ac2e1b24afeb4b195929c7a60335eff5847483fa3d39b151

See more details on using hashes here.

File details

Details for the file customizable-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: customizable-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.9

File hashes

Hashes for customizable-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 989499bd1bea3a2867b16c19c9c6491240578a8da3f79173c515978372aaaa66
MD5 ad69ffc4acdb663ea5b5695a98572e93
BLAKE2b-256 a82d3291e2364e9d7f6d235c8990ccd3dc585d23b1533a61628d7861f18c1e54

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