Base_factory

This module contains functionality related to the the base_factory module for core.

Base_factory

ConfigurationRegistry

Bases: Registry

A specialized registry for configuration classes.

This class extends the Registry to provide additional functionality specific to managing configuration classes, particularly the ability to create Union types from all registered configurations. The type is used by pydantic to validate the given configuration.

Source code in src/core/base_factory.py
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class ConfigurationRegistry(Registry):
    """
    A specialized registry for configuration classes.

    This class extends the Registry to provide additional functionality
    specific to managing configuration classes, particularly the ability
    to create Union types from all registered configurations.
    The type is used by pydantic to validate the given configuration.
    """

    @classmethod
    def get_union_type(cls) -> Any:
        """
        Create a Union type of all registered configuration classes.

        This is useful for type hints where any of the registered
        configuration types can be accepted.

        Returns:
            Any: A Union type containing all registered configuration classes.
        """
        return Union[tuple(cls._objects.values())]

get_union_type() classmethod

Create a Union type of all registered configuration classes.

This is useful for type hints where any of the registered configuration types can be accepted.

Returns:
  • Any( Any ) –

    A Union type containing all registered configuration classes.

Source code in src/core/base_factory.py
211
212
213
214
215
216
217
218
219
220
221
222
@classmethod
def get_union_type(cls) -> Any:
    """
    Create a Union type of all registered configuration classes.

    This is useful for type hints where any of the registered
    configuration types can be accepted.

    Returns:
        Any: A Union type containing all registered configuration classes.
    """
    return Union[tuple(cls._objects.values())]

Factory

Bases: ABC

Abstract Factory base class for creating instances based on configurations.

This class implements the Factory pattern and provides a standardized way to create instances of objects based on their configuration.

Attributes:
  • _configuration_class (Type) –

    The expected type of configuration objects.

Source code in src/core/base_factory.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Factory(ABC):
    """
    Abstract Factory base class for creating instances based on configurations.

    This class implements the Factory pattern and provides a standardized way
    to create instances of objects based on their configuration.

    Attributes:
        _configuration_class (Type): The expected type of configuration objects.
    """

    _configuration_class: Type = None

    @classmethod
    @abstractmethod
    def _create_instance(cls, configuration: Any) -> Any:
        """
        Abstract method that must be implemented by subclasses to create instances.

        Args:
            configuration (Any): The configuration object used to create the instance.

        Returns:
            Any: A new instance created based on the provided configuration.
        """
        pass

    @classmethod
    def create(cls, configuration: Any) -> Any:
        """
        Create an instance based on the given configuration.

        Args:
            configuration (Any): The configuration object used to create the instance. It has to be of the type specified by _configuration_class.

        Returns:
            Any: A new instance created based on the provided configuration.

        Raises:
            ValueError: If the configuration is not an instance of the expected type.
        """
        if not isinstance(configuration, cls._configuration_class):
            raise ValueError(
                f"Given configuration is {type(configuration)}, but must be of type type: {cls._configuration_class}"
            )
        return cls._create_instance(configuration)

create(configuration) classmethod

Create an instance based on the given configuration.

Parameters:
  • configuration (Any) –

    The configuration object used to create the instance. It has to be of the type specified by _configuration_class.

Returns:
  • Any( Any ) –

    A new instance created based on the provided configuration.

Raises:
  • ValueError

    If the configuration is not an instance of the expected type.

Source code in src/core/base_factory.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
@classmethod
def create(cls, configuration: Any) -> Any:
    """
    Create an instance based on the given configuration.

    Args:
        configuration (Any): The configuration object used to create the instance. It has to be of the type specified by _configuration_class.

    Returns:
        Any: A new instance created based on the provided configuration.

    Raises:
        ValueError: If the configuration is not an instance of the expected type.
    """
    if not isinstance(configuration, cls._configuration_class):
        raise ValueError(
            f"Given configuration is {type(configuration)}, but must be of type type: {cls._configuration_class}"
        )
    return cls._create_instance(configuration)

Registry

A registry for storing and retrieving objects by key.

This class implements a simple registry pattern that allows objects to be registered with a key and retrieved later using that key. It is designed to be subclassed, with each subclass defining its own expected key type. It is used for registering different plugins or components in a system.

Attributes:
  • _key_class (Type) –

    The expected type for registry keys.

  • _objects (Dict[Any, Any]) –

    Dictionary storing registered objects by their keys.

Source code in src/core/base_factory.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class Registry:
    """
    A registry for storing and retrieving objects by key.

    This class implements a simple registry pattern that allows objects to be
    registered with a key and retrieved later using that key.
    It is designed to be subclassed, with each subclass defining its own
    expected key type.
    It is used for registering different plugins or components in a system.

    Attributes:
        _key_class (Type): The expected type for registry keys.
        _objects (Dict[Any, Any]): Dictionary storing registered objects by their keys.
    """

    _key_class: Type = None
    _objects: Dict[Any, Any] = {}

    def __init_subclass__(cls, **kwargs: Any):
        """
        Initialize a new subclass with an empty objects dictionary.

        This method is called when a subclass of Registry is created,
        ensuring each subclass has its own independent registry.

        Args:
            **kwargs: Additional keyword arguments.
        """
        super().__init_subclass__(**kwargs)
        cls._objects = {}

    @classmethod
    def register(cls, key: Any, Any: Any) -> None:
        """
        Register an object with the specified key.

        Args:
            key (Any): The key to associate with the object. Must be of type _key_class.
            Any (Any): The object to register.

        Raises:
            ValueError: If the key is not an instance of the expected type.
        """
        if not isinstance(key, cls._key_class):
            raise ValueError(f"Key must be of type: {cls._key_class}")
        cls._objects[key] = Any

    @classmethod
    def get(cls, key: Any) -> Any:
        """
        Retrieve an object by its key.

        Args:
            key (Any): The key associated with the object to retrieve.

        Returns:
            Any: The object associated with the key.

        Raises:
            ValueError: If no object is registered with the given key.
        """
        if key not in cls._objects:
            raise ValueError(f"Factory for '{key}' key is not registered.")
        return cls._objects[key]

    @classmethod
    def get_all(cls) -> Dict[Any, Any]:
        """
        Get all registered objects.

        Returns:
            Dict[Any, Any]: A dictionary containing all registered objects with their keys.
        """
        return cls._objects

__init_subclass__(**kwargs)

Initialize a new subclass with an empty objects dictionary.

This method is called when a subclass of Registry is created, ensuring each subclass has its own independent registry.

Parameters:
  • **kwargs (Any, default: {} ) –

    Additional keyword arguments.

Source code in src/core/base_factory.py
143
144
145
146
147
148
149
150
151
152
153
154
def __init_subclass__(cls, **kwargs: Any):
    """
    Initialize a new subclass with an empty objects dictionary.

    This method is called when a subclass of Registry is created,
    ensuring each subclass has its own independent registry.

    Args:
        **kwargs: Additional keyword arguments.
    """
    super().__init_subclass__(**kwargs)
    cls._objects = {}

get(key) classmethod

Retrieve an object by its key.

Parameters:
  • key (Any) –

    The key associated with the object to retrieve.

Returns:
  • Any( Any ) –

    The object associated with the key.

Raises:
  • ValueError

    If no object is registered with the given key.

Source code in src/core/base_factory.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
@classmethod
def get(cls, key: Any) -> Any:
    """
    Retrieve an object by its key.

    Args:
        key (Any): The key associated with the object to retrieve.

    Returns:
        Any: The object associated with the key.

    Raises:
        ValueError: If no object is registered with the given key.
    """
    if key not in cls._objects:
        raise ValueError(f"Factory for '{key}' key is not registered.")
    return cls._objects[key]

get_all() classmethod

Get all registered objects.

Returns:
  • Dict[Any, Any]

    Dict[Any, Any]: A dictionary containing all registered objects with their keys.

Source code in src/core/base_factory.py
190
191
192
193
194
195
196
197
198
@classmethod
def get_all(cls) -> Dict[Any, Any]:
    """
    Get all registered objects.

    Returns:
        Dict[Any, Any]: A dictionary containing all registered objects with their keys.
    """
    return cls._objects

register(key, Any) classmethod

Register an object with the specified key.

Parameters:
  • key (Any) –

    The key to associate with the object. Must be of type _key_class.

  • Any (Any) –

    The object to register.

Raises:
  • ValueError

    If the key is not an instance of the expected type.

Source code in src/core/base_factory.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
@classmethod
def register(cls, key: Any, Any: Any) -> None:
    """
    Register an object with the specified key.

    Args:
        key (Any): The key to associate with the object. Must be of type _key_class.
        Any (Any): The object to register.

    Raises:
        ValueError: If the key is not an instance of the expected type.
    """
    if not isinstance(key, cls._key_class):
        raise ValueError(f"Key must be of type: {cls._key_class}")
    cls._objects[key] = Any

SingletonFactory

Bases: Factory

This class extends the Factory pattern with Singleton functionality, ensuring that only one instance is created for each unique configuration during a single runtime.

Attributes:
  • _cache (dict) –

    Dictionary storing instances by their configurations.

Source code in src/core/base_factory.py
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class SingletonFactory(Factory):
    """
    This class extends the Factory pattern with Singleton functionality,
    ensuring that only one instance is created for each unique configuration
    during a single runtime.

    Attributes:
        _cache (dict): Dictionary storing instances by their configurations.
    """

    _cache: dict = {}

    def __init_subclass__(cls, **kwargs: Any):
        """
        Initialize a new subclass with an empty cache.

        This method is called when a subclass of SingletonFactory is created,
        ensuring each subclass has its own independent cache.

        Args:
            **kwargs: Additional keyword arguments.
        """
        super().__init_subclass__(**kwargs)
        cls._cache = {}

    @classmethod
    def create(cls, configuration: Any) -> Any:
        """
        Create or retrieve a singleton instance based on the given configuration.

        Args:
            configuration (Any): The configuration object used to create/retrieve the instance. It has to be of the type specified by _configuration_class.

        Returns:
            Any: A singleton instance associated with the provided configuration.

        Raises:
            ValueError: If the configuration is not an instance of the expected type.
        """
        if not isinstance(configuration, cls._configuration_class):
            raise ValueError(
                f"Given configuration is {type(configuration)}, but must be of type type: {cls._configuration_class}"
            )
        return cls._create_singleton(configuration)

    @classmethod
    def _create_singleton(cls, configuration: Any) -> Any:
        """
        Create a new instance or return an existing one from the cache.

        Args:
            configuration (Any): The configuration object used to create/retrieve the instance.

        Returns:
            Any: A singleton instance associated with the provided configuration.
        """
        if configuration in cls._cache:
            return cls._cache[configuration]
        instance = cls._create_instance(configuration)
        cls._cache[configuration] = instance
        return instance

    @classmethod
    def clear_cache(cls):
        """
        Clear the singleton instance cache.

        This method removes all cached instances, allowing them to be garbage collected.
        """
        cls._cache.clear()

__init_subclass__(**kwargs)

Initialize a new subclass with an empty cache.

This method is called when a subclass of SingletonFactory is created, ensuring each subclass has its own independent cache.

Parameters:
  • **kwargs (Any, default: {} ) –

    Additional keyword arguments.

Source code in src/core/base_factory.py
65
66
67
68
69
70
71
72
73
74
75
76
def __init_subclass__(cls, **kwargs: Any):
    """
    Initialize a new subclass with an empty cache.

    This method is called when a subclass of SingletonFactory is created,
    ensuring each subclass has its own independent cache.

    Args:
        **kwargs: Additional keyword arguments.
    """
    super().__init_subclass__(**kwargs)
    cls._cache = {}

clear_cache() classmethod

Clear the singleton instance cache.

This method removes all cached instances, allowing them to be garbage collected.

Source code in src/core/base_factory.py
115
116
117
118
119
120
121
122
@classmethod
def clear_cache(cls):
    """
    Clear the singleton instance cache.

    This method removes all cached instances, allowing them to be garbage collected.
    """
    cls._cache.clear()

create(configuration) classmethod

Create or retrieve a singleton instance based on the given configuration.

Parameters:
  • configuration (Any) –

    The configuration object used to create/retrieve the instance. It has to be of the type specified by _configuration_class.

Returns:
  • Any( Any ) –

    A singleton instance associated with the provided configuration.

Raises:
  • ValueError

    If the configuration is not an instance of the expected type.

Source code in src/core/base_factory.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@classmethod
def create(cls, configuration: Any) -> Any:
    """
    Create or retrieve a singleton instance based on the given configuration.

    Args:
        configuration (Any): The configuration object used to create/retrieve the instance. It has to be of the type specified by _configuration_class.

    Returns:
        Any: A singleton instance associated with the provided configuration.

    Raises:
        ValueError: If the configuration is not an instance of the expected type.
    """
    if not isinstance(configuration, cls._configuration_class):
        raise ValueError(
            f"Given configuration is {type(configuration)}, but must be of type type: {cls._configuration_class}"
        )
    return cls._create_singleton(configuration)