Skip to content

Commit 24188b1

Browse files
committed
Implement dunder str and repr methods in Value
Add constants for each of the 4 Value classes
1 parent 0ed8f26 commit 24188b1

File tree

10 files changed

+83
-5
lines changed

10 files changed

+83
-5
lines changed

src/pybind/bool_values.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from abc import ABC
44

5-
from pybind.values import Value, DerivedValue, _S, _T
5+
from pybind.values import Value, DerivedValue, _S, _T, Constant
66

77

88
class BoolValue(Value[bool], ABC):
@@ -16,3 +16,11 @@ def __init__(self, value: Value[bool]):
1616

1717
def transform(self, value: bool) -> bool:
1818
return not value
19+
20+
21+
class BoolConstant(BoolValue, Constant[bool]):
22+
pass
23+
24+
25+
TRUE = BoolConstant(True)
26+
FALSE = BoolConstant(False)

src/pybind/float_values.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from pybind.observables import Observer, ValueObserver
1010

1111
from pybind.values import Value, CombinedMixedValues, SimpleVariable, CombinedTwoValues, _create_value_getter, \
12-
DerivedValue, DerivedValueBase
13-
12+
DerivedValue, DerivedValueBase, Constant
1413

1514
FloatLike = int | Value[int] | float | Value[float]
1615

@@ -59,6 +58,9 @@ def __ge__(self, other: FloatLike) -> BoolValue:
5958
def __neg__(self) -> FloatValue:
6059
return NegateFloatValue(self)
6160

61+
class FloatConstant(FloatValue, Constant[float]):
62+
pass
63+
6264

6365
def _create_float_getter(value: float | Value[int] | Value[float]) -> Callable[[], float]:
6466
if isinstance(value, Value):

src/pybind/int_values.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from pybind.float_values import FloatValue, MultiplyFloatValues, DivideValues, SubtractFloatValues, \
88
AddFloatValues, CompareNumbersValues
9-
from pybind.values import Value, CombinedMixedValues, SimpleVariable, CombinedTwoValues, DerivedValue
9+
from pybind.values import Value, CombinedMixedValues, SimpleVariable, CombinedTwoValues, DerivedValue, Constant
1010
from pybind.bool_values import BoolValue
1111

1212
IntLike = int | Value[int]
@@ -108,6 +108,10 @@ def __neg__(self) -> IntValue:
108108
return NegateIntValue(self)
109109

110110

111+
class IntConstant(IntValue, Constant[int]):
112+
pass
113+
114+
111115
class IntVariable(SimpleVariable[int], IntValue):
112116
pass
113117

src/pybind/str_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 Any
55

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

88
StringLike = str | Value[str]
99

@@ -16,6 +16,10 @@ def __radd__(self, other: StringLike) -> StrValue:
1616
return ConcatenateStrValues(other, self)
1717

1818

19+
class StrConstant(StrValue, Constant[str]):
20+
pass
21+
22+
1923
class StrVariable(SimpleVariable[str], StrValue):
2024
pass
2125

src/pybind/values.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def to_str(self) -> StrValue:
5858
from pybind.str_values import ToStrValue
5959
return ToStrValue(self)
6060

61+
def __str__(self) -> str:
62+
return str(self.value)
63+
64+
def __repr__(self) -> str:
65+
return f"{self.__class__.__name__}({self.value!r})"
66+
6167

6268
class Variable(Value[_S], Generic[_S], ABC):
6369
@property
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from pybind.str_values import StrConstant
2+
3+
4+
def test_str_constant_str():
5+
const = StrConstant("hello")
6+
assert str(const) == "hello"
7+

tests/test_boo_values.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from pybind.bool_values import BoolConstant
2+
3+
4+
def test_bool_constant_str():
5+
const = BoolConstant(True)
6+
assert str(const) == "True"
7+
8+
const_false = BoolConstant(False)
9+
assert str(const_false) == "False"

tests/test_float_values.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pybind.float_values import FloatConstant
2+
3+
4+
def test_float_constant_str():
5+
const = FloatConstant(3.14)
6+
assert str(const) == "3.14"

tests/test_int_values.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from pybind.int_values import IntConstant
2+
3+
4+
def test_int_constant_str():
5+
const = IntConstant(42)
6+
assert str(const) == "42"

tests/test_simple_variable.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,29 @@ def test_daisy_chain_variables_weak_reference_stays():
383383
root_var.value = "root2"
384384

385385
assert values == ["root1", "root2"]
386+
387+
388+
def test_simple_int_variable_str():
389+
var_int = SimpleVariable(42)
390+
assert str(var_int) == "42"
391+
392+
393+
def test_simple_str_variable_str():
394+
var_str = SimpleVariable("hello")
395+
assert str(var_str) == "hello"
396+
397+
398+
def test_simple_float_variable_str():
399+
var_float = SimpleVariable(3.14)
400+
assert str(var_float) == "3.14"
401+
402+
403+
def test_simple_bool_variable_str():
404+
var_bool = SimpleVariable(True)
405+
assert str(var_bool) == "True"
406+
407+
408+
def test_simple_none_variable_str():
409+
none_bool = SimpleVariable(None)
410+
assert str(none_bool) == "None"
411+

0 commit comments

Comments
 (0)