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]]
) –
|
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
|