A registry for storing and retrieving objects by key.
This class implements a simple registry pattern that allows objects to be
registered with a key and retrieved later using that key.
It is designed to be subclassed, with each subclass defining its own
expected key type.
It is used for registering different plugins or components in a system.
Attributes: |
-
_key_class
(Type )
–
The expected type for registry keys.
-
_objects
(Dict[Any, Any] )
–
Dictionary storing registered objects by their keys.
|
Source code in src/core/base_factory.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198 | class Registry:
"""
A registry for storing and retrieving objects by key.
This class implements a simple registry pattern that allows objects to be
registered with a key and retrieved later using that key.
It is designed to be subclassed, with each subclass defining its own
expected key type.
It is used for registering different plugins or components in a system.
Attributes:
_key_class (Type): The expected type for registry keys.
_objects (Dict[Any, Any]): Dictionary storing registered objects by their keys.
"""
_key_class: Type = None
_objects: Dict[Any, Any] = {}
def __init_subclass__(cls, **kwargs: Any):
"""
Initialize a new subclass with an empty objects dictionary.
This method is called when a subclass of Registry is created,
ensuring each subclass has its own independent registry.
Args:
**kwargs: Additional keyword arguments.
"""
super().__init_subclass__(**kwargs)
cls._objects = {}
@classmethod
def register(cls, key: Any, Any: Any) -> None:
"""
Register an object with the specified key.
Args:
key (Any): The key to associate with the object. Must be of type _key_class.
Any (Any): The object to register.
Raises:
ValueError: If the key is not an instance of the expected type.
"""
if not isinstance(key, cls._key_class):
raise ValueError(f"Key must be of type: {cls._key_class}")
cls._objects[key] = Any
@classmethod
def get(cls, key: Any) -> Any:
"""
Retrieve an object by its key.
Args:
key (Any): The key associated with the object to retrieve.
Returns:
Any: The object associated with the key.
Raises:
ValueError: If no object is registered with the given key.
"""
if key not in cls._objects:
raise ValueError(f"Factory for '{key}' key is not registered.")
return cls._objects[key]
@classmethod
def get_all(cls) -> Dict[Any, Any]:
"""
Get all registered objects.
Returns:
Dict[Any, Any]: A dictionary containing all registered objects with their keys.
"""
return cls._objects
|
__init_subclass__(**kwargs)
Initialize a new subclass with an empty objects dictionary.
This method is called when a subclass of Registry is created,
ensuring each subclass has its own independent registry.
Parameters: |
-
**kwargs
(Any , default:
{}
)
–
Additional keyword arguments.
|
Source code in src/core/base_factory.py
143
144
145
146
147
148
149
150
151
152
153
154 | def __init_subclass__(cls, **kwargs: Any):
"""
Initialize a new subclass with an empty objects dictionary.
This method is called when a subclass of Registry is created,
ensuring each subclass has its own independent registry.
Args:
**kwargs: Additional keyword arguments.
"""
super().__init_subclass__(**kwargs)
cls._objects = {}
|
get(key)
classmethod
Retrieve an object by its key.
Parameters: |
-
key
(Any )
–
The key associated with the object to retrieve.
|
Returns: |
-
Any ( Any
) –
The object associated with the key.
|
Raises: |
-
ValueError
–
If no object is registered with the given key.
|
Source code in src/core/base_factory.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 | @classmethod
def get(cls, key: Any) -> Any:
"""
Retrieve an object by its key.
Args:
key (Any): The key associated with the object to retrieve.
Returns:
Any: The object associated with the key.
Raises:
ValueError: If no object is registered with the given key.
"""
if key not in cls._objects:
raise ValueError(f"Factory for '{key}' key is not registered.")
return cls._objects[key]
|
get_all()
classmethod
Get all registered objects.
Returns: |
-
Dict[Any, Any]
–
Dict[Any, Any]: A dictionary containing all registered objects with their keys.
|
Source code in src/core/base_factory.py
190
191
192
193
194
195
196
197
198 | @classmethod
def get_all(cls) -> Dict[Any, Any]:
"""
Get all registered objects.
Returns:
Dict[Any, Any]: A dictionary containing all registered objects with their keys.
"""
return cls._objects
|
register(key, Any)
classmethod
Register an object with the specified key.
Parameters: |
-
key
(Any )
–
The key to associate with the object. Must be of type _key_class.
-
Any
(Any )
–
|
Raises: |
-
ValueError
–
If the key is not an instance of the expected type.
|
Source code in src/core/base_factory.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 | @classmethod
def register(cls, key: Any, Any: Any) -> None:
"""
Register an object with the specified key.
Args:
key (Any): The key to associate with the object. Must be of type _key_class.
Any (Any): The object to register.
Raises:
ValueError: If the key is not an instance of the expected type.
"""
if not isinstance(key, cls._key_class):
raise ValueError(f"Key must be of type: {cls._key_class}")
cls._objects[key] = Any
|