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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()
Functions¤
__init__() ¤

Initialize the objects dictionary as empty.

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

Create the object from the args.

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

Get all registered objects.

Source code in trestle/core/object_factory.py
42
43
44
def get_all(self) -> ValuesView[Any]:
    """Get all registered objects."""
    return self._objects.values()
register_object(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
29
30
31
32
33
34
35
36
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