preheat_open.interfaces.Factory

class preheat_open.interfaces.Factory(input_dict={})

Bases: ABC

Abstract factory class for creating objects from dictionaries.

The Factory pattern provides a way to create objects from configuration dictionaries, with automatic translation of keys and validation of unused parameters.

Parameters:

input_dict (dict[str, Any]) – Dictionary containing configuration data

Variables:
  • _return_class – Class to instantiate

  • _translation_dict – Mapping of attribute names to dictionary keys

Example:
>>> # Create a simple factory example
>>> class SimpleFactory(Factory):
...     def __init__(self, input_dict=None):
...         super().__init__(input_dict or {})
...         self._return_class = dict
...         self._translation_dict = {"key": "value"}
>>>
>>> factory = SimpleFactory({"value": "test"})
>>> isinstance(factory.input_dict, dict)
True
__init__(input_dict={})

Initialize the factory with input configuration.

Parameters:

input_dict (dict[str, Any]) – Dictionary containing configuration data

Example:
>>> factory = Factory.__new__(Factory)
>>> factory.__init__({"test": "value"})
>>> factory.input_dict
{'test': 'value'}

Methods

__init__([input_dict])

Initialize the factory with input configuration.

build(**kwargs)

Build the target object from the configuration dictionary.

make_sub_classes()

Create any necessary sub-objects before building the main object.

translate(obj)

Translate an object back to its dictionary representation.

final build(**kwargs)

Build the target object from the configuration dictionary.

This method processes the input dictionary, creates any necessary sub-objects, and instantiates the target class with the translated parameters.

Parameters:

kwargs – Additional keyword arguments to merge with input_dict

Returns:

Instance of the target class

Return type:

Any

Raises:

TypeError – If translation fails

Example:
>>> class TestClass:
...     def __init__(self, name):
...         self.name = name
>>>
>>> class TestFactory(Factory):
...     def __init__(self, input_dict=None):
...         super().__init__(input_dict or {})
...         self._return_class = TestClass
...         self._translation_dict = {"name": "test_name"}
>>>
>>> factory = TestFactory({"test_name": "example"})
>>> obj = factory.build()
>>> obj.name
'example'
make_sub_classes()

Create any necessary sub-objects before building the main object.

This method is called before building the main object and allows the factory to create any dependent objects or modify the input dictionary as needed. :rtype: None

Note

Override this method in subclasses to implement custom sub-object creation logic.

classmethod translate(obj)

Translate an object back to its dictionary representation.

This class method reverses the build process, converting an object back to the dictionary format expected by the factory.

Parameters:

obj – Object to translate

Returns:

Dictionary representation of the object

Return type:

Any

Raises:

TypeError – If object is not of the expected type

Example:
>>> class TestClass:
...     def __init__(self, name):
...         self.name = name
>>>
>>> class TestFactory(Factory):
...     _return_class = TestClass
...     _translation_dict = {"name": "test_name"}
>>>
>>> obj = TestClass("example")
>>> result = TestFactory.translate(obj)
>>> result
{'test_name': 'example'}