Inject metadata into objects without them knowing.

Registry

A class to register and retrieve types and their signatures. It acts as collection of types and is usefull in cases where a python object needs to be created dynamically based on a string name.

Attributes:
  • types (dict) –

    a dictionary of registered types.

  • signatures (dict) –

    a dictionary of registered types signatures.

Methods:

Name Description
register

a decorator to register a type.

get

get a registered type by name.

keys

get the list of registered type names.

signature

get the signature of a registered type by.

Example:

.. code-block:: python

from mlregistry.registry import Registry

registry = Registry()

@registry.register
class Foo:
    def __init__(self, x: int, y: float, z: str):
        self.x = x
        self.y = y
        self.z = z

instance = registry.get('Foo')(1, 2.0, '3') # instance of Foo
signature = registry.signature('Foo') # {'x': 'int', 'y': 'float', 'z': 'str'}
keys = registry.keys() # ['Foo']

get(name)

Get a registered type by name from the registry.

Parameters:
  • name (str) –

    the name of the type to be retrieved

Returns:
  • Optional[type[T]]

    Optional[type[T]]: the registered type if found, otherwise None

keys()

Get the list of registered type names.

Returns:
  • list[str]

    list[str]: the list of registered type names

signature(name)

Get the signature of a registered type by name.

Parameters:
  • name (str) –

    the name of the type to be retrieved.

Returns:
  • Optional[dict[str, str]]

    dict[str, str]: the signature of the registered type.

register(cls, excluded_args=None, excluded_kwargs=None)

register(cls: type, excluded_args: list[int] = None, excluded_kwargs: set[str] = None)
register(cls: str, excluded_args: list[int] = None, excluded_kwargs: set[str] = None)

A function to override the init method of a class in order to capture the arguments passed to it when an instance of the given type is initialized. Can be used as a raw decorator or as a decorator with a name argument to set the name of the class in the registry.

Parameters:
  • cls (type | str | None) –

    The class to override the init method or the name of the class in the registry.

  • excluded_args (list[int], default: None ) –

    The indexes of the arguments to exclude from the capture. Defaults to None.

  • excluded_kwargs (set[str], default: None ) –

    The names of the keyword arguments to exclude from the capture. Defaults to None.

Returns:
  • type

    The class with the init method overriden or a decorator to override the init method of a class.