element.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. from dataclasses import dataclass, asdict, astuple
  2. from typing import TypeVar, Generic, Optional
  3. # Some typing information for Python static type checkers
  4. Literal = TypeVar('Literal', int, float, bool, str) # Must be int, float, bool or str
  5. @dataclass
  6. class Element(Generic[Literal]):
  7. """
  8. An Element can represent one of following two things, based on the value of its attributes:
  9. * An element (node or edge) in the State (id is not None). In this case the value can be None because it hasn't
  10. yet been read OR because the element doesn't have a value.
  11. * A value for which a node has not yet been materialized in the State (id is None, value is not None).
  12. If you are familiar with the Modelverse Kernel, this class serves a function similar to the {id: ..., value: ...}
  13. dict that is used there.
  14. """
  15. id: Optional[str] = None
  16. value: Optional[Literal] = None
  17. def is_none(self) -> bool:
  18. return self.id is None and self.value is None
  19. String = Element[str]
  20. Integer = Element[int]
  21. Float = Element[float]
  22. Boolean = Element[bool]