Skip to content

object_factory

trestle.core.object_factory ¤

Generic object factory.

Classes¤

ObjectFactory ¤

Allow registration and creation of factory objects.

Source code in trestle/core/object_factory.py
class ObjectFactory:
    """Allow registration and creation of factory objects."""

    def __init__(self) -> None:
        """Initialize the objects dictionary as empty."""
        self._objects: Dict[str, Any] = {}

    def register_object(self, mode: str, obj: Any) -> None:
        """Register an object to the object factory.

        Args:
            mode: Descriptive key for the mode / type of object to be retrieved.
            obj: The object type to be registered.
        """
        self._objects[mode] = obj

    def get(self, args: argparse.Namespace) -> Any:
        """Create the object from the args."""
        return self._objects.get(args.mode)

    def get_all(self) -> ValuesView[Any]:
        """Get all registered objects."""
        return self._objects.values()
Methods¤
__init__(self) special ¤

Initialize the objects dictionary as empty.

Source code in trestle/core/object_factory.py
def __init__(self) -> None:
    """Initialize the objects dictionary as empty."""
    self._objects: Dict[str, Any] = {}
get(self, args) ¤

Create the object from the args.

Source code in trestle/core/object_factory.py
def get(self, args: argparse.Namespace) -> Any:
    """Create the object from the args."""
    return self._objects.get(args.mode)
get_all(self) ¤

Get all registered objects.

Source code in trestle/core/object_factory.py
def get_all(self) -> ValuesView[Any]:
    """Get all registered objects."""
    return self._objects.values()
register_object(self, mode, obj) ¤

Register an object to the object factory.

Parameters:

Name Type Description Default
mode str

Descriptive key for the mode / type of object to be retrieved.

required
obj Any

The object type to be registered.

required
Source code in trestle/core/object_factory.py
def register_object(self, mode: str, obj: Any) -> None:
    """Register an object to the object factory.

    Args:
        mode: Descriptive key for the mode / type of object to be retrieved.
        obj: The object type to be registered.
    """
    self._objects[mode] = obj

handler: python