Skip to content

Commit 49e81fa

Browse files
committed
Add string representation and test
1 parent adf125d commit 49e81fa

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/dmu/stats/measurement.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ class Measurement(BaseModel):
1313
data : dict[str, tuple[float, float]]
1414
model_config = ConfigDict(frozen=True)
1515
# ----------------------
16+
def __repr__(self) -> str:
17+
'''
18+
String representation
19+
'''
20+
message = f'\n{"Variable":<40}{"Value":<20}{"Error":<20}\n'
21+
message+= 80 * '-' + '\n'
22+
for name, (value, error) in self.data.items():
23+
message += f'{name:<40}{value:<20.3f}{error:<20.3f}\n'
24+
25+
return message
26+
# ----------------------
27+
def __str__(self) -> str:
28+
return self.__repr__()
29+
# ----------------------
1630
def __getitem__(self, variable : str) -> tuple[float,float]:
1731
'''
1832
Parameters

tests/stats/test_measurement.py

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

77
from dmu import Measurement
88

9-
9+
# ------------------------------------
1010
def test_simple():
1111
data = {'a' : (1., 1.), 'b' : (2., 1.)}
1212

@@ -18,3 +18,14 @@ def test_simple():
1818

1919
assert 'a' in ms
2020
assert 'x' not in ms
21+
# ------------------------------------
22+
def test_print():
23+
'''
24+
Test string representations
25+
'''
26+
data = {'a' : (1., 1.), 'b' : (2., 1.)}
27+
28+
ms = Measurement(data=data)
29+
30+
ms.__repr__()
31+
ms.__str__()

0 commit comments

Comments
 (0)