Skip to content

Abstract factory

objectory.abstract_factory

Implement the AbstractFactory metaclass used to create abstract factories.

objectory.abstract_factory.AbstractFactory

Bases: ABCMeta

Implement the abstract factory metaclass to create automatically factories.

Please read the documentation about this abstract factory to learn how it works and how to use it.

To avoid potential conflicts with the other classes, all the non-public attributes or functions starts with _abstractfactory_**** where **** is the name of the attribute or the function.

Parameters:

Name Type Description Default
name str

Specifies the class name. This becomes the __name__ attribute of the class.

required
bases tuple

Specifies a tuple of the base classes from which the class inherits. This becomes the __bases__ attribute of the class.

required
dct dict

Specifies a namespace dictionary containing definitions for the class body. This becomes the __dict__ attribute of the class.

required

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass(BaseClass):
...     pass
...
>>> obj = BaseClass.factory("MyClass")
>>> obj
<....MyClass object at 0x...>

objectory.abstract_factory.AbstractFactory.inheritors property

inheritors: dict[str, Any]

Get the inheritors.

Returns:

Type Description
dict[str, Any]

The inheritors.

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass(BaseClass):
...     pass
...
>>> BaseClass.inheritors
{'....BaseClass': <class '....BaseClass'>, '....MyClass': <class '....MyClass'>}

objectory.abstract_factory.AbstractFactory.factory

factory(
    _target_: str,
    *args: Any,
    _init_: str = "__init__",
    **kwargs: Any
) -> Any

Instantiate dynamically an object given its configuration.

Please read the documentation for more information.

Parameters:

Name Type Description Default
_target_ str

Specifies the name of the object (class or function) to instantiate. It can be the class name or the full class name.

required
*args Any

Variable length argument list.

()
_init_ str

Specifies the function to use to create the object. If "__init__", the object is created by calling the constructor.

'__init__'
**kwargs Any

Arbitrary keyword arguments.

{}

Returns:

Type Description
Any

The instantiated object with the given parameters.

Raises:

Type Description
AbstractClassAbstractFactoryError

if you try to instantiate an abstract class.

UnregisteredClassAbstractFactoryError

if the target is not found.

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass(BaseClass):
...     pass
...
>>> obj = BaseClass.factory("MyClass")
>>> obj
<....MyClass object at 0x...>

objectory.abstract_factory.AbstractFactory.register_object

register_object(obj: type | Callable) -> None

Register a class or function to the factory. It is useful if you are using a 3rd party library.

For example, you use a 3rd party library, and you cannot modify the classes to add AbstractFactory. You can use this function to register some classes or functions of a 3rd party library.

Parameters:

Name Type Description Default
obj type | Callable

Specifies the class or function to register to the factory.

required

Raises:

Type Description
IncorrectObjectAbstractFactoryError

if the object is not a class.

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass:
...     pass
...
>>> BaseClass.register_object(MyClass)
>>> BaseClass.inheritors
{...}

objectory.abstract_factory.AbstractFactory.unregister

unregister(name: str) -> None

Remove a registered object from the factory.

This is an experimental function and may change in the future.

Parameters:

Name Type Description Default
name str

Specifies the name of the object to remove. This function uses the name resolution mechanism to find the full name if only the short name is given.

required

Example usage:

>>> from objectory import AbstractFactory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> class MyClass:
...     pass
...
>>> BaseClass.register_object(MyClass)
>>> BaseClass.unregister("MyClass")
>>> BaseClass.inheritors
{'....BaseClass': <class '....BaseClass'>}

objectory.abstract_factory.is_abstract_factory

is_abstract_factory(cls: Any) -> bool

Indicate if a class implements the AbstractFactory metaclass.

Parameters:

Name Type Description Default
cls Any

Specifies the class to check.

required

Returns:

Type Description
bool

True if the class implements the AbstractFactory metaclass, otherwise False.

Example usage:

>>> from objectory.abstract_factory import AbstractFactory, is_abstract_factory
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> is_abstract_factory(BaseClass)
True
>>> is_abstract_factory(int)
False

objectory.abstract_factory.register

register(cls: AbstractFactory) -> Callable

Define a decorator to register a function to a factory.

This decorator is designed to register functions that returns an object of a class registered in the factory.

Parameters:

Name Type Description Default
cls AbstractFactory

Specifies the class where to register the function.

required

Returns:

Type Description
Callable

collections.abc.Callable: The decorated function.

Example usage:

>>> from objectory.abstract_factory import AbstractFactory, register
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> @register(BaseClass)
... def function_to_register(value: int) -> int:
...     return value + 2
...
>>> BaseClass.factory("function_to_register", 40)
42

objectory.abstract_factory.register_child_classes

register_child_classes(
    factory_cls: AbstractFactory | type,
    cls: type,
    ignore_abstract_class: bool = True,
) -> None

Register the given class and its child classes of a given class.

This function registers all the child classes including the child classes of the child classes, etc.

Parameters:

Name Type Description Default
factory_cls AbstractFactory | type

Specifies the factory class. The child classes will be registered to this class.

required
cls type

Specifies the class to register its child classes.

required
ignore_abstract_class bool

Indicate if the abstract class should be ignored or not. Be default, the abstract classes are not registered because they cannot be instantiated.

True

Raises:

Type Description
AbstractFactoryTypeError

if the factory class does not implement the AbstractFactory metaclass.

Example usage:

>>> from objectory.abstract_factory import AbstractFactory, register_child_classes
>>> class BaseClass(metaclass=AbstractFactory):
...     pass
...
>>> register_child_classes(BaseClass, dict)