Core Registry#

registry #

CLASS DESCRIPTION
Registry

Generic registry for classes with factory pattern support.

Classes#

Registry #

Generic registry for classes with factory pattern support.

Subclasses must define _base_class and _registry_name class attributes.

ATTRIBUTE DESCRIPTION
_base_class

Base class that all registered classes must inherit from.

TYPE: type

_registry_name

Human-readable name for error messages (e.g., "kernel", "loss").

TYPE: str

_registry

Internal storage for registered classes.

TYPE: dict[str, type]

METHOD DESCRIPTION
register

Register a class with the given name.

build

Instantiate a class from the registry.

get

Get a class from the registry without instantiation.

available

Return a list of available registered names.

is_registered

Check if a name is registered.

Functions#
register(name: str) classmethod #

Register a class with the given name.

The registration name is stored on the class as _registry_key attribute.

Arguments

name : str Name to register the class under.

RETURNS DESCRIPTION
Callable

Decorator that registers the class.

RAISES DESCRIPTION
TypeError

If the decorated class is not a subclass of _base_class.

ValueError

If a class with the given name is already registered.

Source code in spectre/core/registry.py
@classmethod
def register(cls, name: str):
    """
    Register a class with the given name.

    The registration name is stored on the class as `_registry_key` attribute.

    Arguments
    ---------
    name : str
        Name to register the class under.

    Returns
    -------
    Callable
        Decorator that registers the class.

    Raises
    ------
    TypeError
        If the decorated class is not a subclass of `_base_class`.
    ValueError
        If a class with the given name is already registered.
    """

    def decorator(registered_cls: type) -> type:
        if not isinstance(registered_cls, type) or not issubclass(
            registered_cls, cls._base_class
        ):
            raise TypeError(
                f"Registered class must be a {cls._base_class.__name__} subclass, "
                f"got {registered_cls!r}."
            )
        if name in cls._registry:
            raise ValueError(
                f"{cls._registry_name.capitalize()} '{name}' is already registered "
                f"as {cls._registry[name].__name__}."
            )
        cls._registry[name] = registered_cls
        registered_cls._registry_key = name
        return registered_cls

    return decorator
build(name: str, **kwargs) classmethod #

Instantiate a class from the registry.

Arguments

name : str Name of the registered class. **kwargs Keyword arguments passed to the class constructor.

RETURNS DESCRIPTION
object

Instantiated object.

RAISES DESCRIPTION
ValueError

If no class is registered with the given name.

Source code in spectre/core/registry.py
@classmethod
def build(cls, name: str, **kwargs):
    """
    Instantiate a class from the registry.

    Arguments
    ---------
    name : str
        Name of the registered class.
    **kwargs
        Keyword arguments passed to the class constructor.

    Returns
    -------
    object
        Instantiated object.

    Raises
    ------
    ValueError
        If no class is registered with the given name.
    """
    if name not in cls._registry:
        raise ValueError(
            f"Unknown {cls._registry_name}: '{name}'. Available: {cls.available()}."
        )
    return cls._registry[name](**kwargs)
get(name: str) -> type classmethod #

Get a class from the registry without instantiation.

Arguments

name : str Name of the registered class.

RETURNS DESCRIPTION
type

The registered class.

RAISES DESCRIPTION
ValueError

If no class is registered with the given name.

Source code in spectre/core/registry.py
@classmethod
def get(cls, name: str) -> type:
    """
    Get a class from the registry without instantiation.

    Arguments
    ---------
    name : str
        Name of the registered class.

    Returns
    -------
    type
        The registered class.

    Raises
    ------
    ValueError
        If no class is registered with the given name.
    """
    if name not in cls._registry:
        raise ValueError(
            f"Unknown {cls._registry_name}: '{name}'. Available: {cls.available()}."
        )
    return cls._registry[name]
available() -> list[str] classmethod #

Return a list of available registered names.

Source code in spectre/core/registry.py
@classmethod
def available(cls) -> list[str]:
    """Return a list of available registered names."""
    return list(cls._registry.keys())
is_registered(name: str) -> bool classmethod #

Check if a name is registered.

Source code in spectre/core/registry.py
@classmethod
def is_registered(cls, name: str) -> bool:
    """Check if a name is registered."""
    return name in cls._registry