pyh0n/pyhon/parameter/range.py

73 lines
2.2 KiB
Python
Raw Normal View History

2023-04-23 02:38:44 +05:30
from typing import Dict, Any, List
2023-04-16 05:13:37 +05:30
2023-06-13 03:42:29 +05:30
from pyhon.helper import str_to_float
2023-04-16 05:13:37 +05:30
from pyhon.parameter.base import HonParameter
class HonParameterRange(HonParameter):
2023-05-06 19:37:28 +05:30
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
super().__init__(key, attributes, group)
2023-07-12 03:35:27 +05:30
self._min: float = 0
self._max: float = 0
self._step: float = 0
self._default: float = 0
self._value: float = 0
self._set_attributes()
2023-07-12 23:06:32 +05:30
def _set_attributes(self) -> None:
2023-07-12 03:35:27 +05:30
super()._set_attributes()
2023-07-19 01:01:16 +05:30
self._min = str_to_float(self._attributes.get("minimumValue", 0))
self._max = str_to_float(self._attributes.get("maximumValue", 0))
self._step = str_to_float(self._attributes.get("incrementValue", 0))
2023-07-12 23:06:32 +05:30
self._default = str_to_float(self._attributes.get("defaultValue", self.min))
self._value = self._default
2023-04-16 05:13:37 +05:30
2023-06-28 22:32:11 +05:30
def __repr__(self) -> str:
2023-06-08 01:20:26 +05:30
return f"{self.__class__} (<{self.key}> [{self.min} - {self.max}])"
2023-04-16 05:13:37 +05:30
@property
def min(self) -> float:
return self._min
2023-05-28 21:04:32 +05:30
@min.setter
2023-06-08 01:20:26 +05:30
def min(self, mini: float) -> None:
self._min = mini
2023-05-28 21:04:32 +05:30
2023-04-16 05:13:37 +05:30
@property
def max(self) -> float:
return self._max
2023-05-28 21:04:32 +05:30
@max.setter
2023-06-08 01:20:26 +05:30
def max(self, maxi: float) -> None:
self._max = maxi
2023-05-28 21:04:32 +05:30
2023-04-16 05:13:37 +05:30
@property
def step(self) -> float:
2023-05-08 03:33:29 +05:30
if not self._step:
return 1
2023-04-16 05:13:37 +05:30
return self._step
2023-05-28 21:04:32 +05:30
@step.setter
def step(self, step: float) -> None:
self._step = step
2023-04-16 05:13:37 +05:30
@property
2023-05-28 09:47:43 +05:30
def value(self) -> str | float:
2023-06-08 01:20:26 +05:30
return self._value if self._value is not None else self.min
2023-04-16 05:13:37 +05:30
@value.setter
2023-05-28 09:47:43 +05:30
def value(self, value: str | float) -> None:
2023-04-16 05:13:37 +05:30
value = str_to_float(value)
2023-07-01 19:34:34 +05:30
if self.min <= value <= self.max and not ((value - self.min) * 100) % (
self.step * 100
):
2023-04-16 05:13:37 +05:30
self._value = value
2023-05-21 05:55:43 +05:30
self.check_trigger(value)
2023-04-16 05:13:37 +05:30
else:
allowed = f"min {self.min} max {self.max} step {self.step}"
raise ValueError(f"Allowed: {allowed} But was: {value}")
2023-04-23 02:38:44 +05:30
@property
def values(self) -> List[str]:
return [str(i) for i in range(int(self.min), int(self.max) + 1, int(self.step))]