Skip to content

Factory API

sweet_tea.factory.Factory

Bases: BaseFactory

Base factory class for creating instances of registered classes.

This factory provides the core functionality for instantiating classes from the registry with optional filtering by library and label.

Source code in sweet_tea/factory.py
class Factory(BaseFactory):
    """
    Base factory class for creating instances of registered classes.

    This factory provides the core functionality for instantiating classes
    from the registry with optional filtering by library and label.
    """

    _logger = logging.getLogger(__name__)

    @classmethod
    def create(
        cls,
        key: str,
        library: str = "",
        label: str = "",
        configuration: dict[str, Any] | BaseModel | None = None,
    ) -> Any:
        """
        Create an instance of a registered class.

        Args:
            key: Name to reference the class from the registry.
            library: Optional library filter for the class.
            label: Optional label filter for the class.
            configuration: Configuration parameters as keyword arguments, given as a
                dict or a pydantic model whose fields are spread into the constructor.

        Returns:
            Configured instance of the requested class.

        Raises:
            SweetTeaError: When the key is not found or filters don't match.
        """
        return cls._create_from_entries(
            cls._find_entries(key), key, library, label, configuration
        )

    @classmethod
    def _create_from_entries(
        cls,
        entries: list[Entry],
        key: str,
        library: str,
        label: str,
        configuration: dict[str, Any] | BaseModel | None,
    ) -> Any:
        """
        Create an instance from a filtered list of entries.

        Args:
            entries: List of candidate entries.
            key: The requested key.
            library: Library filter (empty string means no filter).
            label: Label filter (empty string means no filter).
            configuration: Configuration parameters.

        Returns:
            Instantiated and configured class instance.

        Raises:
            SweetTeaError: When no matching entry is found or multiple entries remain after filtering.
        """
        entry = cls._select_entry(entries, key, library, label)
        return cls._construct(entry, configuration)

create(key, library='', label='', configuration=None) classmethod

Create an instance of a registered class.

Parameters:

Name Type Description Default
key str

Name to reference the class from the registry.

required
library str

Optional library filter for the class.

''
label str

Optional label filter for the class.

''
configuration dict[str, Any] | BaseModel | None

Configuration parameters as keyword arguments, given as a dict or a pydantic model whose fields are spread into the constructor.

None

Returns:

Type Description
Any

Configured instance of the requested class.

Raises:

Type Description
SweetTeaError

When the key is not found or filters don't match.

Source code in sweet_tea/factory.py
@classmethod
def create(
    cls,
    key: str,
    library: str = "",
    label: str = "",
    configuration: dict[str, Any] | BaseModel | None = None,
) -> Any:
    """
    Create an instance of a registered class.

    Args:
        key: Name to reference the class from the registry.
        library: Optional library filter for the class.
        label: Optional label filter for the class.
        configuration: Configuration parameters as keyword arguments, given as a
            dict or a pydantic model whose fields are spread into the constructor.

    Returns:
        Configured instance of the requested class.

    Raises:
        SweetTeaError: When the key is not found or filters don't match.
    """
    return cls._create_from_entries(
        cls._find_entries(key), key, library, label, configuration
    )

Methods

create()

sweet_tea.factory.Factory.create(key, library='', label='', configuration=None) classmethod

Create an instance of a registered class.

Parameters:

Name Type Description Default
key str

Name to reference the class from the registry.

required
library str

Optional library filter for the class.

''
label str

Optional label filter for the class.

''
configuration dict[str, Any] | BaseModel | None

Configuration parameters as keyword arguments, given as a dict or a pydantic model whose fields are spread into the constructor.

None

Returns:

Type Description
Any

Configured instance of the requested class.

Raises:

Type Description
SweetTeaError

When the key is not found or filters don't match.

Source code in sweet_tea/factory.py
@classmethod
def create(
    cls,
    key: str,
    library: str = "",
    label: str = "",
    configuration: dict[str, Any] | BaseModel | None = None,
) -> Any:
    """
    Create an instance of a registered class.

    Args:
        key: Name to reference the class from the registry.
        library: Optional library filter for the class.
        label: Optional label filter for the class.
        configuration: Configuration parameters as keyword arguments, given as a
            dict or a pydantic model whose fields are spread into the constructor.

    Returns:
        Configured instance of the requested class.

    Raises:
        SweetTeaError: When the key is not found or filters don't match.
    """
    return cls._create_from_entries(
        cls._find_entries(key), key, library, label, configuration
    )