pyhOn/pyhon/parameter/range.py

61 lines
1.8 KiB
Python
Raw Normal View History

2023-04-22 21:08:44 +00:00
from typing import Dict, Any, List
2023-04-15 23:43:37 +00:00
2023-06-12 22:12:29 +00:00
from pyhon.helper import str_to_float
2023-04-15 23:43:37 +00:00
from pyhon.parameter.base import HonParameter
class HonParameterRange(HonParameter):
2023-05-06 14:07:28 +00:00
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
super().__init__(key, attributes, group)
2023-04-15 23:43:37 +00: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"])
2023-06-07 19:50:26 +00:00
self._default: float = str_to_float(attributes.get("defaultValue", self.min))
2023-04-15 23:43:37 +00:00
self._value: float = self._default
2023-06-28 17:02:11 +00:00
def __repr__(self) -> str:
2023-06-07 19:50:26 +00:00
return f"{self.__class__} (<{self.key}> [{self.min} - {self.max}])"
2023-04-15 23:43:37 +00:00
@property
def min(self) -> float:
return self._min
2023-05-28 15:34:32 +00:00
@min.setter
2023-06-07 19:50:26 +00:00
def min(self, mini: float) -> None:
self._min = mini
2023-05-28 15:34:32 +00:00
2023-04-15 23:43:37 +00:00
@property
def max(self) -> float:
return self._max
2023-05-28 15:34:32 +00:00
@max.setter
2023-06-07 19:50:26 +00:00
def max(self, maxi: float) -> None:
self._max = maxi
2023-05-28 15:34:32 +00:00
2023-04-15 23:43:37 +00:00
@property
def step(self) -> float:
2023-05-07 22:03:29 +00:00
if not self._step:
return 1
2023-04-15 23:43:37 +00:00
return self._step
2023-05-28 15:34:32 +00:00
@step.setter
def step(self, step: float) -> None:
self._step = step
2023-04-15 23:43:37 +00:00
@property
2023-05-28 04:17:43 +00:00
def value(self) -> str | float:
2023-06-07 19:50:26 +00:00
return self._value if self._value is not None else self.min
2023-04-15 23:43:37 +00:00
@value.setter
2023-05-28 04:17:43 +00:00
def value(self, value: str | float) -> None:
2023-04-15 23:43:37 +00:00
value = str_to_float(value)
2023-06-07 19:50:26 +00:00
if self.min <= value <= self.max and not (value - self.min) % self.step:
2023-04-15 23:43:37 +00:00
self._value = value
2023-05-21 00:25:43 +00:00
self.check_trigger(value)
2023-04-15 23:43:37 +00:00
else:
2023-06-07 19:50:26 +00:00
raise ValueError(f"Allowed: min {self.min} max {self.max} step {self.step}")
2023-04-22 21:08:44 +00:00
@property
def values(self) -> List[str]:
return [str(i) for i in range(int(self.min), int(self.max) + 1, int(self.step))]