Added Element dataclass
This commit is contained in:
parent
34881ad4f4
commit
6d652c213d
1 changed files with 31 additions and 0 deletions
31
core/element.py
Normal file
31
core/element.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
from dataclasses import dataclass, asdict, astuple
|
||||||
|
from typing import TypeVar, Generic, Optional
|
||||||
|
|
||||||
|
# Some typing information for Python static type checkers
|
||||||
|
Literal = TypeVar('Literal', int, float, bool, str) # Must be int, float, bool or str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Element(Generic[Literal]):
|
||||||
|
"""
|
||||||
|
An Element can represent one of following two things, based on the value of its attributes:
|
||||||
|
|
||||||
|
* An element (node or edge) in the State (id is not None). In this case the value can be None because it hasn't
|
||||||
|
yet been read OR because the element doesn't have a value.
|
||||||
|
* A value for which a node has not yet been materialized in the State (id is None, value is not None).
|
||||||
|
|
||||||
|
|
||||||
|
If you are familiar with the Modelverse Kernel, this class serves a function similar to the {id: ..., value: ...}
|
||||||
|
dict that is used there.
|
||||||
|
"""
|
||||||
|
id: Optional[str] = None
|
||||||
|
value: Optional[Literal] = None
|
||||||
|
|
||||||
|
def is_none(self) -> bool:
|
||||||
|
return self.id is None and self.value is None
|
||||||
|
|
||||||
|
|
||||||
|
String = Element[str]
|
||||||
|
Integer = Element[int]
|
||||||
|
Float = Element[float]
|
||||||
|
Boolean = Element[bool]
|
||||||
Loading…
Add table
Add a link
Reference in a new issue