noether.core.factory.base¶
Classes¶
Base factory. Implements base structures for creating a single object, a list of objects and a dict |
Module Contents¶
- class noether.core.factory.base.Factory(returns_partials=False)¶
Base factory. Implements base structures for creating a single object, a list of objects and a dict of objects.
For example, creating a list:
class Example: def __init__(self, callbacks: list[CallbackConfig] | None = None): # automatic none check in create_list (this is how Factory is implemented) self.callbacks = Factory().create_list(callbacks) # required none check after creating the list (this is how one could implement it without create_list)
Objects can be instantiated from either:
BaseModel: In this case, thekindfield is used to determine the class path of the object to be instantiated. The full pydantic model is passed to the constructor.class ExampleConfig(pydantic.BaseModel): kind: str param1: int param2: str class ExampleObject: def __init__(self, config: ExampleConfig): self.param1 = config.param1 self.param2 = config.param2 # kind is also in the config, but usually not needed in the object itself example_config = ExampleConfig(kind="path.to.ExampleObject", param1=42, param2="hello") example_object = Factory().create(example_config)
Using
BaseModelis the preferred way of instantiating objects as it provides type safety and validation.Dictionary: In this case, the
kindkey is used to determine the class path of the object to be instantiated. The full dictionary is passed to the constructor. However, almost all classes in Noether take a config object as input to the constructor. This approach will only work for custom classes that take named arguments in the constructor.example_dict = {"kind": "path.to.ExampleObject", "param1": 42, "param2": "hello"} class ExampleObject: # constructor takes named arguments directly, and kind popped before passing to constructor def __init__(self, param1: int, param2: str): self.param1 = param1 self.param2 = param2 example_object = Factory().create(example_dict)
- Parameters:
returns_partials (bool)
- logger¶
- returns_partials = False¶
- create(obj_or_kwargs, **kwargs)¶
Creates an object if the object is specified as dictionary. If the object was already instantiated, it will simply return the existing object. If
obj_or_kwargsisNone,Noneis returned.- Parameters:
obj_or_kwargs (Any | dict[str, Any] | pydantic.BaseModel | None) – Either an existing object or a description of how an object should be instantiated (dict or
pydantic.BaseModel).kwargs – Further kwargs that are passed when creating the object. These are often dependencies such as
UpdateCounter,PathProvider,MetricPropertyProvider, etc.
- Returns:
The instantiated object.
- Return type:
Any | None
- create_list(collection, **kwargs)¶
Creates a list of objects by calling the
create()function for every item in the collection.If
collectionisNone, an empty list is returned.- Parameters:
collection (list[Any] | list[dict[str, Any]] | dict[str, Any] | list[pydantic.BaseModel] | None) – Either a list of configs how the objects should be instantiated.
kwargs – Further kwargs that are passed to all object instantiations. These are often dependencies such as
UpdateCounter,PathProvider,MetricPropertyProvider, etc.
- Returns:
The instantiated list of objects or an empty list.
- Return type:
list[Any]
- create_dict(collection, **kwargs)¶
Creates a dict of objects by calling the
create()function for every item in the collection.If
collectionisNone, an empty dictionary is returned.- Parameters:
collection (dict[str, Any] | dict[str, dict[str, Any]] | None) – Either a dict of existing objects or a dict of descriptions how the objects should be instantiated and what their identifier in the dict is.
kwargs – Further kwargs that are passed to all object instantiations. These are often dependencies such as
UpdateCounter,PathProvider,MetricPropertyProvider, etc.
- Returns:
The instantiated dict of objects or an empty dict.
- Return type:
- instantiate(object_config=None, **kwargs)¶
Instantiates an object based on its fully specified classpath.
- Parameters:
object_config (pydantic.BaseModel | None) –
- Configuration containing the fully specified type of the object in the
kindfield. For example:
"torch.optim.SGD"or"noether.core.callbacks.CheckpointCallback".
kwargs: kwargs passed to the type when instantiating the object.
- Configuration containing the fully specified type of the object in the
- Returns:
The instantiated object.
- Return type:
Any