Source code for vivyd.data.value

from __future__ import annotations

from dataclasses import dataclass
from typing import Any
from quantities import UnitQuantity, Quantity


[docs] @dataclass(kw_only=True) class Value: """ A class representing a value, which is a single data point with associated units. Parameters ---------- name : str The name of the value. data : float | str | bool The actual value. units : UnitQuantity | Quantity The units of the value, as defined by the ``quantities`` library. """ name : str data : float | str | bool units: UnitQuantity | Quantity @property def __dict__(self) -> dict[str, Any]: return { "name" : self.name, "data" : self.data, "units": self.units.dimensionality.string, "type" : self.__class__.__name__ } @staticmethod def _from_dict(data: dict[str, Any]) -> Value: return Value( name = data["name"], data = data["data"], units = Quantity(1.0, data["units"]).units )
[docs] def tree(self, indent: int = 2) -> str: """ Parameters ---------- indent : int, optional The number of spaces to indent the tree representation of the value. Default is 2. Returns ------- str The tree representation of the value. """ type_ = self.__class__.__name__ units_ = self.units.dimensionality.string return f"{' ' * indent}<{type_}> {self.name} [{units_}] {self.data}"