Skip to content

base

logger = logging.getLogger(__name__) module-attribute

T = TypeVar('T', bound=BaseModel) module-attribute

Registry

Bases: Generic[T], BaseModel

A Registry is a wrapper for a dictionary that maps names to Python types.

Most often, this is used to map names to classes.

Source code in src/recnexteval/registries/base.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
class Registry(Generic[T], BaseModel):
    """A Registry is a wrapper for a dictionary that maps names to Python types.

    Most often, this is used to map names to classes.
    """

    def __init__(self, src: ModuleType) -> None:
        self.registered: dict[str, type[T]] = {}
        self.src = src
        self._register_all_src()

    def _register_all_src(self) -> None:
        """Register all classes from the src module."""
        if not hasattr(self.src, "__all__"):
            raise AttributeError(f"Source module {self.src} has no __all__ attribute")
        if self.src.__all__ is None:
            raise AttributeError(f"Source module {self.src} has __all__ set to None")
        for class_name in self.src.__all__:
            try:
                cls = getattr(self.src, class_name)
                if not inspect.isclass(cls):
                    continue
                self.register(class_name, cls)
            except AttributeError:
                # Skip if the attribute doesn't exist
                continue

    def __getitem__(self, key: str) -> type[T]:
        """Retrieve the type for the given key.

        Args:
            key: The key of the type to fetch.

        Returns:
            The class type associated with the key.

        Raises:
            KeyError: If the key is not found in the registry.
        """
        try:
            return self.registered[key]
        except KeyError:
            raise KeyError(f"key `{key}` not found in registry")

    def __contains__(self, key: str) -> bool:
        """Check if the given key is known to the registry.

        Args:
            key: The key to check.

        Returns:
            True if the key is known, False otherwise.
        """
        try:
            self[key]
            return True
        except KeyError:
            return False

    def get(self, key: str) -> type[T]:
        """Retrieve the value for this key.

        This value is a Python type, most often a class.

        Args:
            key: The key to fetch.

        Returns:
            The class type associated with the key.

        Raises:
            KeyError: If the key is not found in the registry.
        """
        return self[key]

    def register(self, key: str, cls: type[T]) -> None:
        """Register a new Python type (most often a class).

        After registration, the key can be used to fetch the Python type from the registry.

        Args:
            key: Key to register the type at. Needs to be unique to the registry.
            cls: Class to register.

        Raises:
            KeyError: If the key is already registered.
        """
        if key in self.registered:
            raise KeyError(f"key `{key}` already registered")
        self.registered[key] = cls

    def get_registered_keys(self, include_base: bool = False) -> list[str]:
        """Get a list of all registered keys.

        Returns:
            A list of all registered keys.
        """
        if include_base:
            return list(self.registered.keys())
        else:
            return [key for key, cls in self.registered.items() if not getattr(cls, "IS_BASE", True)]

    def registered_values(self) -> list[type[T]]:
        """Get a list of all registered types.

        Returns:
            A list of all registered types.
        """
        return [self.registered[key] for key in self.get_registered_keys(include_base=False)]

    def registered_items(self) -> list[tuple[str, type[T]]]:
        """Get a list of all registered key-type pairs.

        Returns:
            A list of all registered key-type pairs.
        """
        return [(key, self.registered[key]) for key in self.get_registered_keys(include_base=False)]

registered = {} instance-attribute

src = src instance-attribute

IS_BASE = True class-attribute instance-attribute

name property

Name of the object's class.

:return: Name of the object's class :rtype: str

get(key)

Retrieve the value for this key.

This value is a Python type, most often a class.

Parameters:

Name Type Description Default
key str

The key to fetch.

required

Returns:

Type Description
type[T]

The class type associated with the key.

Raises:

Type Description
KeyError

If the key is not found in the registry.

Source code in src/recnexteval/registries/base.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def get(self, key: str) -> type[T]:
    """Retrieve the value for this key.

    This value is a Python type, most often a class.

    Args:
        key: The key to fetch.

    Returns:
        The class type associated with the key.

    Raises:
        KeyError: If the key is not found in the registry.
    """
    return self[key]

register(key, cls)

Register a new Python type (most often a class).

After registration, the key can be used to fetch the Python type from the registry.

Parameters:

Name Type Description Default
key str

Key to register the type at. Needs to be unique to the registry.

required
cls type[T]

Class to register.

required

Raises:

Type Description
KeyError

If the key is already registered.

Source code in src/recnexteval/registries/base.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def register(self, key: str, cls: type[T]) -> None:
    """Register a new Python type (most often a class).

    After registration, the key can be used to fetch the Python type from the registry.

    Args:
        key: Key to register the type at. Needs to be unique to the registry.
        cls: Class to register.

    Raises:
        KeyError: If the key is already registered.
    """
    if key in self.registered:
        raise KeyError(f"key `{key}` already registered")
    self.registered[key] = cls

get_registered_keys(include_base=False)

Get a list of all registered keys.

Returns:

Type Description
list[str]

A list of all registered keys.

Source code in src/recnexteval/registries/base.py
106
107
108
109
110
111
112
113
114
115
def get_registered_keys(self, include_base: bool = False) -> list[str]:
    """Get a list of all registered keys.

    Returns:
        A list of all registered keys.
    """
    if include_base:
        return list(self.registered.keys())
    else:
        return [key for key, cls in self.registered.items() if not getattr(cls, "IS_BASE", True)]

registered_values()

Get a list of all registered types.

Returns:

Type Description
list[type[T]]

A list of all registered types.

Source code in src/recnexteval/registries/base.py
117
118
119
120
121
122
123
def registered_values(self) -> list[type[T]]:
    """Get a list of all registered types.

    Returns:
        A list of all registered types.
    """
    return [self.registered[key] for key in self.get_registered_keys(include_base=False)]

registered_items()

Get a list of all registered key-type pairs.

Returns:

Type Description
list[tuple[str, type[T]]]

A list of all registered key-type pairs.

Source code in src/recnexteval/registries/base.py
125
126
127
128
129
130
131
def registered_items(self) -> list[tuple[str, type[T]]]:
    """Get a list of all registered key-type pairs.

    Returns:
        A list of all registered key-type pairs.
    """
    return [(key, self.registered[key]) for key in self.get_registered_keys(include_base=False)]