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: |
|
---|
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: |
|
---|
Returns: |
|
---|
keys()
Get the list of registered type names.
Returns: |
|
---|
signature(name)
Get the signature of a registered type by name.
Parameters: |
|
---|
Returns: |
|
---|
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: |
|
---|
Returns: |
|
---|