simulation.utils.basics.init_options module

Summary

Classes:

InitOptions

Reference

class InitOptions[source]

Bases: object

classmethod from_dict(**kwargs: Dict[str, Any])[source]

Create instance from relevant keywords in dictionary.

Instead of passing exactly the arguments defined by the class’ __init__, this allows to pass a dictionary of many more values and the function will then only pass the correct arguments to the class’ __init__.

Example

>>> from simulation.utils.basics.init_options import InitOptions
>>> class SomeClass(InitOptions):
...
...     def __init__(self, arg1, arg2):
...         self.arg1 = arg1
...         self.arg2 = arg2
...
...
>>> many_args = {"arg1": 1, "arg2": 2, "arg3": 3}
... # This will raise an exception because __init__ does not expect arg3!
>>> try:
...     SomeClass(**many_args)
... except TypeError:
...     pass
... # However this will work:
>>> something = SomeClass.from_dict(**many_args)
Parameters

**kwargs – Keyword arguments matching the constructor’s variables.

_from_yaml(file_path: str)[source]

Helper method to allow passing a custom cls instead of being forced to use the classmethod syntax.

classmethod from_yaml(file_path: str)[source]

Load instance from a yaml file.

Only fields that are defined within the __init__ are loaded.

Parameters

file_path – Location of the yaml file.