Bases: KiaraModel
Python class and module information.
Source code in kiara/models/python_class.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 | class PythonClass(KiaraModel):
"""Python class and module information."""
_kiara_model_id = "instance.wrapped_python_class"
@classmethod
def from_class(cls, item_cls: Type, attach_context_metadata: bool = False):
cls_name = item_cls.__name__
module_name = item_cls.__module__
if module_name == "builtins":
full_name = cls_name
else:
full_name = f"{item_cls.__module__}.{item_cls.__name__}"
conf: Dict[str, Any] = {
"python_class_name": cls_name,
"python_module_name": module_name,
"full_name": full_name,
}
if attach_context_metadata:
raise NotImplementedError()
ctx_md = ContextMetadataModel.from_class(item_cls=item_cls)
conf["items"] = ctx_md
result = PythonClass.construct(**conf)
result._cls_cache = item_cls
return result
python_class_name: str = Field(description="The name of the Python class.")
python_module_name: str = Field(
description="The name of the Python module this class lives in."
)
full_name: str = Field(description="The full class namespace.")
# context_metadata: Optional[ContextMetadataModel] = Field(
# description="Context metadata for the class.", default=None
# )
_module_cache: ModuleType = PrivateAttr(default=None)
_cls_cache: Type = PrivateAttr(default=None)
def _retrieve_id(self) -> str:
return self.full_name
def _retrieve_data_to_hash(self) -> Any:
return self.full_name
def get_class(self) -> Type:
if self._cls_cache is None:
m = self.get_python_module()
self._cls_cache = getattr(m, self.python_class_name)
return self._cls_cache
def get_python_module(self) -> ModuleType:
if self._module_cache is None:
self._module_cache = importlib.import_module(self.python_module_name)
return self._module_cache
|
Attributes
python_class_name: str = Field(description='The name of the Python class.')
class-attribute
python_module_name: str = Field(description='The name of the Python module this class lives in.')
class-attribute
full_name: str = Field(description='The full class namespace.')
class-attribute
Functions
from_class(item_cls: Type, attach_context_metadata: bool = False)
classmethod
Source code in kiara/models/python_class.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 | @classmethod
def from_class(cls, item_cls: Type, attach_context_metadata: bool = False):
cls_name = item_cls.__name__
module_name = item_cls.__module__
if module_name == "builtins":
full_name = cls_name
else:
full_name = f"{item_cls.__module__}.{item_cls.__name__}"
conf: Dict[str, Any] = {
"python_class_name": cls_name,
"python_module_name": module_name,
"full_name": full_name,
}
if attach_context_metadata:
raise NotImplementedError()
ctx_md = ContextMetadataModel.from_class(item_cls=item_cls)
conf["items"] = ctx_md
result = PythonClass.construct(**conf)
result._cls_cache = item_cls
return result
|
get_class() -> Type
Source code in kiara/models/python_class.py
| def get_class(self) -> Type:
if self._cls_cache is None:
m = self.get_python_module()
self._cls_cache = getattr(m, self.python_class_name)
return self._cls_cache
|
get_python_module() -> ModuleType
Source code in kiara/models/python_class.py
| def get_python_module(self) -> ModuleType:
if self._module_cache is None:
self._module_cache = importlib.import_module(self.python_module_name)
return self._module_cache
|