2023-04-22 15:08:44 -06:00
|
|
|
from typing import Dict, Any, List
|
2023-04-15 17:43:37 -06:00
|
|
|
|
|
|
|
from pyhon.parameter.base import HonParameter
|
|
|
|
|
|
|
|
|
|
|
|
def str_to_float(string: str | float) -> float:
|
|
|
|
try:
|
|
|
|
return int(string)
|
|
|
|
except ValueError:
|
|
|
|
return float(str(string).replace(",", "."))
|
|
|
|
|
|
|
|
|
|
|
|
class HonParameterRange(HonParameter):
|
2023-05-06 08:07:28 -06:00
|
|
|
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
|
|
|
|
super().__init__(key, attributes, group)
|
2023-04-15 17:43:37 -06:00
|
|
|
self._min: float = str_to_float(attributes["minimumValue"])
|
|
|
|
self._max: float = str_to_float(attributes["maximumValue"])
|
|
|
|
self._step: float = str_to_float(attributes["incrementValue"])
|
|
|
|
self._default: float = str_to_float(attributes.get("defaultValue", self._min))
|
|
|
|
self._value: float = self._default
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f"{self.__class__} (<{self.key}> [{self._min} - {self._max}])"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def min(self) -> float:
|
|
|
|
return self._min
|
|
|
|
|
2023-05-28 09:34:32 -06:00
|
|
|
@min.setter
|
|
|
|
def min(self, min: float) -> None:
|
|
|
|
self._min = min
|
|
|
|
|
2023-04-15 17:43:37 -06:00
|
|
|
@property
|
|
|
|
def max(self) -> float:
|
|
|
|
return self._max
|
|
|
|
|
2023-05-28 09:34:32 -06:00
|
|
|
@max.setter
|
|
|
|
def max(self, max: float) -> None:
|
|
|
|
self._max = max
|
|
|
|
|
2023-04-15 17:43:37 -06:00
|
|
|
@property
|
|
|
|
def step(self) -> float:
|
2023-05-07 16:03:29 -06:00
|
|
|
if not self._step:
|
|
|
|
return 1
|
2023-04-15 17:43:37 -06:00
|
|
|
return self._step
|
|
|
|
|
2023-05-28 09:34:32 -06:00
|
|
|
@step.setter
|
|
|
|
def step(self, step: float) -> None:
|
|
|
|
self._step = step
|
|
|
|
|
2023-04-15 17:43:37 -06:00
|
|
|
@property
|
2023-05-27 22:17:43 -06:00
|
|
|
def value(self) -> str | float:
|
2023-04-15 17:43:37 -06:00
|
|
|
return self._value if self._value is not None else self._min
|
|
|
|
|
|
|
|
@value.setter
|
2023-05-27 22:17:43 -06:00
|
|
|
def value(self, value: str | float) -> None:
|
2023-04-15 17:43:37 -06:00
|
|
|
value = str_to_float(value)
|
2023-05-14 15:03:46 -06:00
|
|
|
if self._min <= value <= self._max and not (value - self._min) % self._step:
|
2023-04-15 17:43:37 -06:00
|
|
|
self._value = value
|
2023-05-20 18:25:43 -06:00
|
|
|
self.check_trigger(value)
|
2023-04-15 17:43:37 -06:00
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
f"Allowed: min {self._min} max {self._max} step {self._step}"
|
|
|
|
)
|
2023-04-22 15:08:44 -06:00
|
|
|
|
|
|
|
@property
|
|
|
|
def values(self) -> List[str]:
|
|
|
|
return [str(i) for i in range(int(self.min), int(self.max) + 1, int(self.step))]
|