Skip to content

registry

A common basic registry object for all type of classes.

This registry can add a class to the registry either by using metaclass=BaseRegistry in a new object or using the wrapper function as a header @BaseRegistry.register(name)

Classes:

BaseRegistry

Bases: type

A generic registry implementation.

registry-subclasses.

Source: https://charlesreid1.github.io/python-patterns-the-registry.html

Methods:

  • all

    Return all the saved classes.

  • get

    Returns the saved classe with a given name as key.

  • register

    Function using a wrapper to register the given class with a specific name.

  • save_class

    Saves the class and the given name in the registry.

all classmethod

all() -> dict[str, type[object]]

Return all the saved classes.

Source code in src/stimulus/core/registry.py
69
70
71
72
@classmethod
def all(cls) -> dict[str, type[object]]:
    """Return all the saved classes."""
    return cls._REGISTRY

get classmethod

get(name: str) -> type[object] | None

Returns the saved classe with a given name as key.

Source code in src/stimulus/core/registry.py
64
65
66
67
@classmethod
def get(cls, name: str) -> type[object] | None:
    """Returns the saved classe with a given name as key."""
    return cls._REGISTRY.get(name.lower())

register classmethod

register(
    name: str | None = None,
) -> Callable[[type[object]], type[object]]

Function using a wrapper to register the given class with a specific name.

Source code in src/stimulus/core/registry.py
50
51
52
53
54
55
56
57
58
59
60
61
62
@classmethod
def register(
    cls,
    name: str | None = None,
) -> Callable[[type[object]], type[object]]:
    """Function using a wrapper to register the given class with a specific name."""

    def wrapper(wrapped_class: type[object]) -> type[object]:
        """Wrapper function to save a class to the registry."""
        cls.save_class(wrapped_class, name)
        return wrapped_class

    return wrapper

save_class classmethod

save_class(
    class_to_register: type[object], name: str | None = None
) -> None

Saves the class and the given name in the registry.

Source code in src/stimulus/core/registry.py
39
40
41
42
43
44
45
46
47
48
@classmethod
def save_class(
    cls,
    class_to_register: type[object],
    name: str | None = None,
) -> None:
    """Saves the class and the given name in the registry."""
    if name is None:
        name = class_to_register.__name__
    cls._REGISTRY[name.lower()] = class_to_register