From 6d652c213da840dbdbc607b3a25ddf8020d0fe63 Mon Sep 17 00:00:00 2001 From: Andrei Bondarenko Date: Tue, 13 Jul 2021 19:49:20 +0200 Subject: [PATCH] Added Element dataclass --- core/element.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 core/element.py diff --git a/core/element.py b/core/element.py new file mode 100644 index 0000000..f428528 --- /dev/null +++ b/core/element.py @@ -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]