class documentation
class AutoRegisterMeta(ABCMeta):
Constructor: AutoRegisterMeta(name, bases, attrs, register, ...)
Metaclass for automatically registering modules.
Can be set as a metaclass for abstract base classes. All subclasses of that class will be automatically registered.
Example
>>> REGISTRY = Registry(name="modules") >>> class Base( ... metaclass=AutoRegisterMeta, ... registry=REGISTRY, ... register=False ... ): ... pass >>> class Foo(Base): ... pass >>> class Bar(Base, register_name="Baz"): ... pass >>> REGISTRY.get("Foo").__name__ 'Foo' >>> Base.REGISTRY.get("Baz").__name__ 'Bar'
| Method | __new__ |
Automatically register the class. |
| Instance Variable | REGISTRY |
The internal registry defined on the base class. |
def __new__(cls, name:
str, bases: tuple[ type, ...], attrs: dict[ str, type], register: bool = True, register_name: str | None = None, registry: Registry | None = None):
¶
Automatically register the class.
| Parameters | |
| cls | Undocumented |
name:str | The name of the class being created. |
bases:tuple[ | The base classes of the class being created. |
attrs:dict[ | The attributes of the class being created. |
register:bool | Whether to register this class. Typically should be set to False for abstract base classes. |
registerstr | None | The name to register the class under. If None, then use the class name. |
registry:Registry | None | The registry to use for registration.
Will be saved as the REGISTRY attribute
of the class if not already set. |
| Returns | |
| The newly created class. |
| Raises | |
ValueError | If registration is requested but no registry is available from the class hierarchy or the registry argument. |