Registry

Sorry, the person who is responsible for the description was eaten by hydras last week.

class hydra_slayer.registry.Registry(meta_factory: Optional[Callable[[Union[Type, Callable[[...], Any]], Tuple, Mapping], Any]] = None, name_key: str = '_target_')[source]

Universal class allowing to add and access various factories by name.

Parameters
  • meta_factory – default object that calls factory. Default: factory.default_meta_factory()

  • name_key – key to use to extract names of the factories from

add(factory: Optional[Union[Type, Callable[[...], Any]]] = None, *factories: Union[Type, Callable[[...], Any]], name: Optional[str] = None, **named_factories: Union[Type, Callable[[...], Any]]) Union[Type, Callable[[...], Any]][source]

Adds factory to registry with it’s __name__ attribute or provided name. Signature is flexible.

Parameters
  • factory – factory instance

  • factories – more instances

  • name – name to use for the first factory instance, if a single instance is passed

  • named_factories – factory and their names as **kwargs

Returns

first factory passed

Raises
  • ValueError – if multiple factories with a single name are provided

  • LookupError – if factory with provided name is already registered

add_from_module(module, prefix: Optional[Union[str, List[str]]] = None, ignore_all: bool = False) None[source]

Adds all factories present in module. If __all__ attribute is present, takes ony what mentioned in it.

Parameters
  • module – module to scan

  • prefix – prefix string for all the module’s factories. If prefix is a list, all values will be treated as aliases

  • ignore_all – if True, ignores __all__ attribute of the module

Raises

TypeError – if prefix is not a list or a string

all() Iterable[str][source]

Returns list with names of all registered items.

get(name: str) Optional[Union[Type, Callable[[...], Any]]][source]

Retrieves factory, without creating any objects with it or raises error.

Parameters

name – factory name

Returns

factory by name

get_from_params(*, shared_params: Optional[Dict[str, Any]] = None, **kwargs) Union[Any, Tuple[Any, Mapping[str, Any]]][source]

Creates instance based in configuration dict with instantiation_fn. If config[name_key] is None, None is returned.

Parameters
  • shared_params – params to pass on all levels in case of recursive creation

  • **kwargs – **kwargs to pass to the factory

Returns

result of calling instantiate_fn(factory, **sub_kwargs)

get_if_str(obj: Union[str, Type, Callable[[...], Any]])[source]

Returns object from the registry if obj type is string.

get_instance(*args, meta_factory: Optional[Callable[[Union[Type, Callable[[...], Any]], Tuple, Mapping], Any]] = None, **kwargs)[source]

Creates instance by calling specified factory with instantiate_fn.

Parameters
  • *args – *args to pass to the factory

  • meta_factory – function that calls factory the right way. Default: factory.default_meta_factory()

  • **kwargs – **kwargs to pass to the factory

Returns

created instance

late_add(cb: Callable[[hydra_slayer.registry.Registry], None])[source]

Allows to prevent cycle imports by delaying some imports till next registry query.

Parameters

cb – callback receives registry and must call it’s methods to register factories