ksuit.factory.base.factory_base¶
Classes¶
Base factory. Implements base structures for creating a single object, a list of objects and a dict |
Module Contents¶
- class ksuit.factory.base.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. The main difference between these methods are the default return values. As python does not like using an empty list/dict as default value for an argument, arguments are by default often None. By differentiating between these three types, one avoids none checks whenever the factory is called. - create: creates an object. - create_list: creates a list of objects. - create_dict: creates a dict of objects.
For example, creating a list ``` class Example:
- def __init__(self, callbacks: list[Callback] | None = None)
# automatic none check in create_list (this is how FactoryBase is implemented) self.callbacks = create_list(callbacks) # required none check after creating the list (this is how one could implement it without create_list) self.callbacks = create(callbacks) or []
- 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_kwargs is None, None is returned.
- Parameters:
obj_or_kwargs (Any | dict[str, Any] | None) – Either an existing object (Any) or a description of how an object should be instantiated (dict[str, Any]).
kwargs – Further kwargs that are passed when creating the object. These are often dependencies such as UpdateCounter, PathProvider, MetricPropertyProvider, …
- Returns:
The instantiated object.
- Return type:
Any | None
- create_list(collection, **kwargs)¶
Creates a list of object by calling the create function for every item in the collection. If collection is None, an empty list is returned.
- Parameters:
- Returns:
The instantiated list of objects or an empty list.
- Return type:
list[Any]
- create_dict(collection, **kwargs)¶
Creates a dict of object by calling the create function for every item in the collection. If collection is None, an empty dictionary is returned.
- Parameters:
collection (dict[str, Any] | dict[str, dict[str, Any]] | None) – Either a dict of existing objects (dict[str, Any]) or a dict of descriptions how the objects should be instantiated and what their identifier in the dict is (dict[str, dict[str, Any]]).
kwargs – Further kwargs that are passed to all object instantiations. These are often dependencies such as UpdateCounter, PathProvider, MetricPropertyProvider, …
- 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 (Any) – Fully specified type of the object. For example: “torch.optim.SGD” or “ksuit.callbacks.CheckpointCallback”.
kwargs – kwargs passed to the type when instantiating the object.
- Returns:
The instantiated object.
- Return type:
Any