Add metadata to objects without them to know it.

Metadata dataclass

Metadata class to store the metadata of an object. It contains the type, hash, name, and arguments of the object. This is useful for tracking the initialization of objects wihtout having to modify them.

Attributes:
  • type (str) –

    the type of the object (should be the same as T, but python does not support this yet)

  • hash (str) –

    the hash of the object

  • name (str) –

    the name of the object

  • arguments (dict[str, Any]) –

    the arguments that were passed to the object during initialization

Registry

Register a type in the registry with metadata factory injected in the init method. When an object that is registered is created, a metadata object is created and attached to the object and is accessible using the functions getmetadata, getsignature, and gethash.

After registering a type, the objects of that type can be recreated from the registry using the get method.

Methods:

Name Description
register

Register a type in a given category. This method injects metadata in the init method of the 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 name

Example:

.. code-block:: python

from mlregistry import Registry

registry = Registry()

class Person:
    def __init__(self, name: str, age: int):
        self.person_name = name
        self.person_age = age

registry.register(Person, 'People')

person = Person('John', 30)

metadata = getmetadata(person)
print(metadata) # Metadata(type='People', hash='h72kasd..as2', name='Person', arguments={'name': 'John', 'age': 30})

__init__(excluded_positions=None, exclude_parameters=None)

Initialize the registry with a dictionary of registered types, their states, excluded positions, and excluded parameters. The excluded positions are the positions are the positions to exclude in the signature of the registered types and the excluded parameters are the parameters to exclude in the signature of the registered types. Make sure to handle both cases when registering a type.

Parameters:
  • types (dict[str, Tuple[type, dict[str, str]]) –

    a dictionary of registered types with their signatures

  • states (dict[str, Any]) –

    a dictionary of the states of the registered types

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

    a list of excluded positions in the signature of the registered types.

  • excluded_parameters (set[str]) –

    a set of excluded parameters in the signature of the registered types.

Example:

.. code-block:: python

from mlregistry import Registry

optimizers = Registry(excluded_positions=[0], exclude_parameters={'params'})

get(name)

Get a registered type by name

Parameters:
  • name (str) –

    the name of the type to be retrieved

Returns:
  • type( Optional[type[T]] ) –

    the registered type

keys()

Get the list of registered type names

Returns:
  • list[str]

    list[str]: the list of registered type names

register(type, category=None)

Register a type in a given category. This method injects metadata in the init method of the type.

Parameters:
  • type (type) –

    the type to be registered

  • category (str, default: None ) –

    the category of the type, should be the same as T, but python does not support this yet

Returns:
  • type( type ) –

    the registered type with metadata factory injected in the init method.

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

getdatehash(datetime)

Get the hash of a datetime object

Parameters:
  • datetime (datetime) –

    the datetime object to get the hash from

Returns:
  • str( str ) –

    the hash of the datetime object

gethash(object, raises=None)

Get the local identifier of an object

Parameters:
  • object (object) –

    the object to get the hash from

Returns:
  • str( str ) –

    the hash of the object if available, or None if no exception is raised

Raises:
  • raises

    If the object does not have a hash and raises is specified

getmetadata(object, raises=None)

Get the metadata of an object.

Parameters:
  • obj (object) –

    The object to retrieve the metadata from.

  • raises (Optional[Type[Exception]], default: None ) –

    An exception to raise if the object does not have metadata.

Returns:
  • Optional[Metadata]

    Optional[Metadata]: The metadata of the object if available, or None if no exception is raised.

Raises:
  • raises

    If the object does not have metadata and raises is specified.

getsignature(object, raises=None)

Get the signature of an object. Return a dictionary with parameter names and annotations.

Parameters:
  • object (object) –

    the object to get the signature from

Returns:
  • dict[str, str]

    dict[str, str]: the signature of the object if available, or None if no exception is raised

Raises:
  • raises

    If the object does not have a signature and raises is specified.