Skip to content

Commit 0ab72e6

Browse files
authored
Add min/max Value classes and tests (#48)
1 parent 45636dc commit 0ab72e6

File tree

5 files changed

+122
-14
lines changed

5 files changed

+122
-14
lines changed

src/spellbind/bool_values.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from abc import ABC
44
from typing import TypeVar, Generic, Callable
55

6-
from spellbind.values import Value, DerivedValue, Constant
6+
from spellbind.values import Value, DerivedValue, Constant, SimpleVariable
77

88
_S = TypeVar('_S')
99

@@ -34,5 +34,9 @@ class BoolConstant(BoolValue, Constant[bool]):
3434
pass
3535

3636

37+
class BoolVariable(SimpleVariable[bool], BoolValue):
38+
pass
39+
40+
3741
TRUE = BoolConstant(True)
3842
FALSE = BoolConstant(False)

src/spellbind/float_values.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from __future__ import annotations
2-
from typing_extensions import Self
32

43
import operator
54
from abc import ABC, abstractmethod
65
from typing import Generic, Callable, Sequence, TypeVar, overload
76

7+
from typing_extensions import Self
88
from typing_extensions import TYPE_CHECKING
99

1010
from spellbind.bool_values import BoolValue
@@ -112,6 +112,10 @@ class FloatConstant(FloatValue, Constant[float]):
112112
pass
113113

114114

115+
class FloatVariable(SimpleVariable[float], FloatValue):
116+
pass
117+
118+
115119
def _create_float_getter(value: float | Value[int] | Value[float]) -> Callable[[], float]:
116120
if isinstance(value, Value):
117121
return lambda: value.value
@@ -158,6 +162,16 @@ def value(self) -> _U:
158162
return self._value
159163

160164

165+
class MaxFloatValues(CombinedFloatValues[float], FloatValue):
166+
def transform(self, values: Sequence[float]) -> float:
167+
return max(values)
168+
169+
170+
class MinFloatValues(CombinedFloatValues[float], FloatValue):
171+
def transform(self, values: Sequence[float]) -> float:
172+
return min(values)
173+
174+
161175
class CombinedTwoFloatValues(CombinedFloatValues[_U], Generic[_U], ABC):
162176
def __init__(self, left: FloatLike, right: FloatLike):
163177
super().__init__(left, right)
@@ -171,9 +185,6 @@ def transform_two(self, left: float, right: float) -> _U:
171185

172186

173187
class AddFloatValues(CombinedFloatValues[float], FloatValue):
174-
def __init__(self, *values: FloatLike):
175-
super().__init__(*values)
176-
177188
def transform(self, values: Sequence[float]) -> float:
178189
return sum(values)
179190

@@ -184,9 +195,6 @@ def transform_two(self, left: float, right: float) -> float:
184195

185196

186197
class MultiplyFloatValues(CombinedFloatValues[float], FloatValue):
187-
def __init__(self, *values: FloatLike):
188-
super().__init__(*values)
189-
190198
def transform(self, values: Sequence[float]) -> float:
191199
result = 1.0
192200
for value in values:
@@ -199,10 +207,6 @@ def transform_two(self, left: float, right: float) -> float:
199207
return left / right
200208

201209

202-
class FloatVariable(SimpleVariable[float], FloatValue):
203-
pass
204-
205-
206210
class RoundFloatValue(CombinedTwoValues[float, int, float], FloatValue):
207211
def __init__(self, value: FloatValue, ndigits: IntLike):
208212
super().__init__(value, ndigits)

src/spellbind/int_values.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ class IntVariable(SimpleVariable[int], IntValue):
148148
pass
149149

150150

151+
class MaxIntValues(CombinedMixedValues[int, int], IntValue):
152+
def transform(self, *values: int) -> int:
153+
return max(values)
154+
155+
156+
class MinIntValues(CombinedMixedValues[int, int], IntValue):
157+
def transform(self, *values: int) -> int:
158+
return min(values)
159+
160+
151161
class AddIntValues(CombinedMixedValues[int, int], IntValue):
152162
def transform(self, *values: int) -> int:
153163
return sum(values)

tests/test_float_values.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
1-
from spellbind.float_values import FloatConstant
1+
from spellbind.float_values import FloatConstant, MaxFloatValues, MinFloatValues
2+
from spellbind.values import SimpleVariable
23

34

45
def test_float_constant_str():
56
const = FloatConstant(3.14)
67
assert str(const) == "3.14"
8+
9+
10+
def test_max_float_values():
11+
a = SimpleVariable(10.5)
12+
b = SimpleVariable(20.3)
13+
c = SimpleVariable(5.7)
14+
15+
max_val = MaxFloatValues(a, b, c)
16+
assert max_val.value == 20.3
17+
18+
a.value = 30.1
19+
assert max_val.value == 30.1
20+
21+
22+
def test_max_float_values_with_literals():
23+
a = SimpleVariable(10.5)
24+
25+
max_val = MaxFloatValues(a, 25.7, 15.2)
26+
assert max_val.value == 25.7
27+
28+
a.value = 30.1
29+
assert max_val.value == 30.1
30+
31+
32+
def test_min_float_values():
33+
a = SimpleVariable(10.5)
34+
b = SimpleVariable(20.3)
35+
c = SimpleVariable(5.7)
36+
37+
min_val = MinFloatValues(a, b, c)
38+
assert min_val.value == 5.7
39+
40+
c.value = 2.1
41+
assert min_val.value == 2.1
42+
43+
44+
def test_min_float_values_with_literals():
45+
a = SimpleVariable(10.5)
46+
47+
min_val = MinFloatValues(a, 25.7, 15.2)
48+
assert min_val.value == 10.5
49+
50+
a.value = 5.1
51+
assert min_val.value == 5.1

tests/test_int_values.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,51 @@
1-
from spellbind.int_values import IntConstant
1+
from spellbind.int_values import IntConstant, MaxIntValues, MinIntValues
2+
from spellbind.values import SimpleVariable
23

34

45
def test_int_constant_str():
56
const = IntConstant(42)
67
assert str(const) == "42"
8+
9+
10+
def test_max_int_values():
11+
a = SimpleVariable(10)
12+
b = SimpleVariable(20)
13+
c = SimpleVariable(5)
14+
15+
max_val = MaxIntValues(a, b, c)
16+
assert max_val.value == 20
17+
18+
a.value = 30
19+
assert max_val.value == 30
20+
21+
22+
def test_max_int_values_with_literals():
23+
a = SimpleVariable(10)
24+
25+
max_val = MaxIntValues(a, 25, 15)
26+
assert max_val.value == 25
27+
28+
a.value = 30
29+
assert max_val.value == 30
30+
31+
32+
def test_min_int_values():
33+
a = SimpleVariable(10)
34+
b = SimpleVariable(20)
35+
c = SimpleVariable(5)
36+
37+
min_val = MinIntValues(a, b, c)
38+
assert min_val.value == 5
39+
40+
c.value = 2
41+
assert min_val.value == 2
42+
43+
44+
def test_min_int_values_with_literals():
45+
a = SimpleVariable(10)
46+
47+
min_val = MinIntValues(a, 25, 15)
48+
assert min_val.value == 10
49+
50+
a.value = 5
51+
assert min_val.value == 5

0 commit comments

Comments
 (0)