Registry

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

class hydra_slayer.registry.Registry(name_key: str = '_target_', var_key: str = '_var_', attrs_delimiter: str = '.')[source]

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

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

  • var_key – key to use to for aliases, aliases let you identify an item and then refer to that item and reuse it multiple times

  • attrs_delimiter – delimiter to use for separation of alias and attribute of an instance to get

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 keyword arguments

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 – keyword arguments to be passed into 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, **kwargs)[source]

Creates instance by calling specified factory with instantiate_fn.

Parameters
  • *args – positional arguments to be passed into the factory

  • **kwargs – keyword arguments to be passed into 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