Python module for registering and instantiating classes by name. Based on the implementation from AllenNLP.
Project description
python-registrable
Python module for registering and instantiating classes by name. Based on the implementation from AllenNLP.
Installing
The quickest way to install is through PyPI.
pip install registrable
Usage
from registrable import Registrable
# Create a base class that inherits from `Registrable`.
class MyBaseClass(Registrable):
def do_something(self):
raise NotImplementedError
# Now register subclass implementations of your base class.
@MyBaseClass.register("first_implementation")
class FirstImplementation(MyBaseClass):
def do_something(self):
return 1
# You can access an implementation by calling `.by_name()` on the base class.
subclass = MyBaseClass.by_name("first_implementation")
instance = subclass()
assert instance.do_something() == 1
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
registrable-0.0.2.tar.gz
(4.0 kB
view hashes)
Built Distribution
Close
Hashes for registrable-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af7cc71fabb1f28cb46ba6ff4acd5788dd18dff92792bff0d64ab7641c10f27c |
|
MD5 | 21bf744de1fac038e89ebe2f5c6913a2 |
|
BLAKE2b-256 | dd65420ef69662c96a6518a9e7f79849cf97e417fa6f1a9196a67cb5a7554958 |