docs(example/base_registrar.py): add example implementation for SpeckleBase class register

re #50
This commit is contained in:
Gergő Jedlicska
2021-02-11 20:34:35 +01:00
parent 3a76358557
commit 5796ff6cdb
+37
View File
@@ -0,0 +1,37 @@
"""This is an example module for automated base model subtype registration."""
from pydantic import BaseModel
from typing import ClassVar, Dict, Type
class ExampleBase(BaseModel):
foo: int
_registry : ClassVar[Dict[str, Type["ExampleBase"]]] = {}
def __init_subclass__(cls, speckle_type:str, **kwargs):
# this requires some validation, there is nothing blocking identical keys...
cls.speckle_type = speckle_type
ExampleBase._registry[speckle_type] = cls
super().__init_subclass__(**kwargs)
class ExampleSub(ExampleBase, speckle_type="sub"):
bar: str
class SpeckleSub(ExampleSub, speckle_type="speckle_sub"):
magic : str
# # uncommenting this raises an error, for missing speckle_type definition
# # this automatically enforces mandatory type definition for everyone
# class FaultySub(ExampleBase):
# pass
if __name__ == "__main__":
for k,v in ExampleBase._registry.items():
print(f"Speckle type {k}, mapped to {v.__name__}")
print(ExampleBase._registry["speckle_sub"](foo=123, magic="trick", bar="baric").json())