Shortcuts

Registry

Registry

Global Catalyst registries subpackage.

class catalyst.registry.registry.Registry(default_name_key: str, default_meta_factory: Callable[[Union[Type, Callable[[...], Any]], Tuple, Mapping], Any] = <function _default_meta_factory>)[source]

Bases: collections.abc.MutableMapping

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

__init__(default_name_key: str, default_meta_factory: Callable[[Union[Type, Callable[[...], Any]], Tuple, Mapping], Any] = <function _default_meta_factory>)[source]
Parameters
  • default_name_key (str) – Default key containing factory name when creating from config

  • default_meta_factory (MetaFactory) – default object that calls factory. Optional. Default just calls factory.

add(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: 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 – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

add_from_module(module, prefix: Union[str, List[str]] = None) → 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 (Union[str, List[str]]) – prefix string for all the module’s factories. If prefix is a list, all values will be treated as aliases.

Raises

TypeError – if prefix is not a list or a string

all() → List[str][source]
Returns

list of names of registered items

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

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

Parameters

name – factory name

Returns

factory by name

Return type

Factory

Raises

RegistryException – if no factory with provided name was registered

get_from_params(*, meta_factory=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
  • meta_factory – Function that calls factory the right way. If not provided, default is used.

  • **kwargs – additional kwargs for factory

Returns

result of calling instantiate_fn(factory, **config)

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

Returns object from the registry if obj type is string.

get_instance(name: str, *args, meta_factory=None, **kwargs)[source]

Creates instance by calling specified factory with instantiate_fn.

Parameters
  • name – factory name

  • meta_factory – Function that calls factory the right way. If not provided, default is used

  • args – args to pass to the factory

  • **kwargs – kwargs to pass to the factory

Returns

created instance

Raises

RegistryException – if could not create object instance

late_add(cb: Callable[[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

len() → int[source]
Returns

length of registered items

exception catalyst.registry.registry.RegistryException(message)[source]

Bases: Exception

Exception class for all registry errors.

__init__(message)[source]

Init.

Parameters

message – exception message

Registries

catalyst.registry.registries.Callback(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Criterion(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Experiment(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Model(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Module(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Optimizer(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Runner(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Sampler(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Scheduler(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present

catalyst.registry.registries.Transform(factory: Union[Type, Callable[[...], Any]] = None, *factories: Union[Type, Callable[[...], Any]], name: str = None, **named_factories: Union[Type, Callable[[...], Any]]) → Union[Type, Callable[[...], Any]]

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

Parameters
  • factory – Factory instance

  • factories – More instances

  • name – Provided name for first instance. Use only when pass single instance.

  • named_factories – Factory and their names as kwargs

Returns

First factory passed

Return type

Factory

Raises

RegistryException – if factory with provided name is already present