Skip to content

CloningFactory

CloningFactory

Bases: Generic[T]

Base class for a factory whose objects are created by a deep copy of registered instances.

related

Attributes

objects instance-attribute

objects = objects or {}

Functions

__getitem__

__getitem__(key: str) -> T

Return the object by the key.

Returns:

Type Description
T

object by the key.

Raises: KeyError: when the key is not in the factory

__init__

__init__(objects: dict[str, T] | None = None) -> None

Constructor.

__iter__

__iter__() -> Iterator[str]

Return the iterator of the factory.

Returns:

Type Description
Iterator[str]

iterator of the factory.

__len__

__len__() -> int

Return the number of objects in the factory.

Returns:

Type Description
int

number of objects in the factory.

__repr__

__repr__() -> str

Return the Python code to construct the factory.

Returns:

Type Description
str

Python code to construct the factory.

__setitem__

__setitem__(key: str, value: T) -> None

Set the value for the key.

Parameters:

Name Type Description Default
key str

name of the object

required
value T

instance to be deep copied

required

__str__

__str__() -> str

Return the class name of the factory.

Returns:

Type Description
str

class name of the factory.

copy

copy(key: str) -> T

Create a deep copy of the object registered by the key.

Parameters:

Name Type Description Default
key str

name of the object

required

Returns:

Type Description
T

deep copy of the object registered by the key

Raises:

Type Description
ValueError

when the key is not registered.