Registry¶
-
class
maze.core.utils.registry.
Registry
(*args, **kwds)¶ Supports the creation of different modules that can be plugged into the environments (like demand generators or reward schemes) and can instantiate them from parameters read from config files.
- Parameters
root_module – Starting point for search for suitable classes to be registered.
base_type – A common interface (parent class) of the registered types (e.g. DemandGenerator)
-
arg_to_collection
(arg: Union[List[Union[None, str, Mapping[str, Any], Any]], Mapping[str, Union[None, str, Mapping[str, Any], Any]]], **kwargs) → Dict[Union[str, int], BaseType]¶ Instantiates objects specified in a list or dictionary.
-
arg_to_obj
(arg: Union[None, str, Mapping[str, Any], Any], config: Mapping[str, Any] = None, **kwargs) → BaseType¶ - Converts arg (usually passed to constructor of an env) to an instantiated class.
If arg is already instantiated, just returns it
If arg is a string, then construct an instance according to given type_registry and config parameters
If arg is a dict-like configuration, construct a new instance. The type is identified by the reserved attribute
type
and the remaining attributes are passed to the constructor.
- Parameters
arg – Either - an instantiated object inheriting from base_type - a string, e.g. ‘static’, usually together with the
config
argument - a dict-like configuration, specifying the type name in the reserved attributetype
together with the constructor arguments. (e.g.{ 'class': 'static_demand', 'constructor_argument': 1, ... }
)config – Config to pass to the constructor if arg is a string.
kwargs – Additional arguments that are merged with the configuration dictionary (useful to sideload objects that can not conveniently be specified in the config, e.g. a shared RandomState)
- Returns
arg if already instantiated, new object otherwise (see the build_obj method above)
-
classmethod
build_obj
(klass_or_callable: Union[Type[BaseType], Callable], instance_or_config: Union[None, str, Mapping[str, Any], Any] = None, **kwargs) → BaseType¶ Given a class, init an instance of that class with given keyword arguments.
- Parameters
klass_or_callable – Class to instantiate, or alternatively a function returning instance of the registry base type class.
instance_or_config – Either already an actual instance of klass or keyword arguments to provide
kwargs – Additional arguments that are merged with the configuration dictionary (useful to sideload objects that can not conveniently be specified in the config, e.g. a shared RandomState)
- Returns
Instance of the given class
-
classmethod
callable_from_path
(path_string: str) → Callable[[…], Any]¶ Attempt to import a callable from the specified path.
- Parameters
path_string – Path to the callable to import.
- Returns
Imported callable.
-
class_type_from_module_name
(module_name: Union[str, Type[BaseType]]) → Type[BaseType]¶ Import the given module and lookup the class from the module with the correct base type.
The implementation expects exactly one matching class per module. A ValueError is returned otherwise. If the module name is not valid, a ModuleNotFoundError is triggered.
- Parameters
module_name – Absolute module path (e.g.
maze_envs.logistics.content_based_replenishment.env.maze_env
)- Returns
The one and only class from the given module that derives from base_type.
-
collect_modules
(root_module: Any, base_type: Type[BaseType])¶ Populates a registry dictionary, by walking the specified root module.
- Parameters
root_module – Starting point for search for suitable classes to be registered.
base_type – Class restriction. Registered classes/modules have to be of type klass.
- Returns
A dictionary with class name -> class type for all registered valid sub-classes.