registrable

registrable.HookType

alias of typing.Callable

class registrable.Registrable

Bases: object

Adapted from allennlp.common.registrable.

Any class that inherits from Registrable gains access to a named registry for its subclasses. To register them, just decorate them with the classmethod @BaseClass.register(name).

After which you can call BaseClass.list_available() to get the keys for the registered subclasses, and BaseClass.by_name(name) to get the corresponding subclass. Note that the registry stores the subclasses themselves; not class instances.

You can specify a default by setting BaseClass.default_implementation. If it is set, it will be the first element of list_available().

Note that if you use this class to implement a new Registrable abstract class, you must ensure that all subclasses of the abstract class are loaded when the module is loaded, because the subclasses register themselves in their respective files. You can achieve this by having the abstract class and all subclasses in the __init__.py of the module in which they reside (as this causes any import of either the abstract class or a subclass to load all other subclasses and the abstract class).

Attributes:
default_implementation

Methods

by_name(name) Get a subclass by its registered name, or its fully qualified class name.
hook(hook, str], None]) Function decorator for adding a default hook to a registrable base class.
is_registered(name) Returns True if name is a registered name.
iter_registered() Iterate through the registered names and subclasses.
list_available() List all registered subclasses.
register(name, override, hooks, str], …) Class decorator for registering a subclass.
classmethod by_name(name: str) → Type[T]

Get a subclass by its registered name, or its fully qualified class name.

Parameters:
name : str
Returns:
Type[T]

The subclass registered under name.

Raises:
RegistrationError

If name is not a registered subclass or valid fully qualified class name.

default_implementation = None

Optional name of default implementation. If specified, the default will be listed first in registrable.Registrable.list_available().

classmethod hook(hook: Callable[[Type[T], str], None])

Function decorator for adding a default hook to a registrable base class.

classmethod is_registered(name: str) → bool

Returns True if name is a registered name.

classmethod iter_registered() → Iterable[Tuple[str, Type[T]]]

Iterate through the registered names and subclasses.

classmethod list_available() → List[str]

List all registered subclasses.

If cls.default_implementation is specified, it will be first in the list.

classmethod register(name: str, override: bool = False, hooks: Optional[List[Callable[[Type[T], str], None]]] = None)

Class decorator for registering a subclass.

Parameters:
name : str

The name to register the subclass under.

override : bool, optional (default = False)

If name is already registered a registrable.exceptions.RegistrationError will be raised unless this is set to True.

hooks : Optional[List[HookType]], optional (default = None)

Hooks to run when the subclass is registered.

Raises:
RegistrationError
registrable.T = ~T

Subclass type.