Subclass Registry¶
- class software_patterns.subclass_registry.SubclassRegistry(*args)[source]¶
Subclass Registry
A (parent) class using this class as metaclass gains the ‘subclasses’ class attribute as well as the ‘create’ and ‘register_as_subclass’ class methods.
The ‘subclasses’ attribute is a python dictionary having string identifiers as keys and subclasses of the (parent) class as values.
The ‘register_as_subclass’ class method can be used as a decorator to indicate that a (child) class should belong in the parent’s class registry. An input string argument will be used as the unique key to register the subclass.
The ‘create’ class method can be invoked with a (string) key and suitable constructor arguments to later construct instances of the corresponding child class.
Example
>>> from software_patterns import SubclassRegistry
>>> class ClassRegistry(metaclass=SubclassRegistry): ... pass
>>> ClassRegistry.subclasses {}
>>> @ClassRegistry.register_as_subclass('child') ... class ChildClass: ... def __init__(self, child_attribute): ... self.attr = child_attribute
>>> child_instance = ClassRegistry.create('child', 'attribute-value') >>> child_instance.attr 'attribute-value'
>>> type(child_instance).__name__ 'ChildClass'
>>> isinstance(child_instance, ChildClass) True
>>> {k: v.__name__ for k, v in ClassRegistry.subclasses.items()} {'child': 'ChildClass'}
- create(subclass_identifier, *args, **kwargs) T [source]¶
Create an instance of a registered subclass, given its unique identifier and runtime (constructor) arguments.
Invokes the identified subclass constructor passing any supplied arguments. The user needs to know the arguments to supply depending on the resulting constructor signature.
- Parameters
subclass_identifier (str) – the unique identifier under which to look for the corresponding subclass
- Raises
UnknownClassError – In case the given identifier is unknown to the parent class
InstantiationError – In case the runtime args and kwargs do not match the constructor signature
- Returns
the instance of the registered subclass
- Return type
object
- register_as_subclass(subclass_identifier)[source]¶
Register a class as subclass of the parent class.
Adds the subclass’ constructor in the registry (dict) under the given (str) identifier. Overrides the registry in case of “identifier collision”. Can be used as a python decorator.
- Parameters
subclass_identifier (str) – the user-defined identifier, under which to register the subclass