Skip to content

Commit 7350b43

Browse files
committed
Add checks for setting res, printer and log of Chronometer
1 parent b39767a commit 7350b43

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/stopuhr/chrono.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,42 @@ def __init__(self, printer: Printer = print, res: int = 2, log: bool = True):
198198
self.log = log
199199
self.reset()
200200

201+
@property
202+
def printer(self) -> Printer:
203+
"""Get the printer function."""
204+
return self._printer
205+
206+
@printer.setter
207+
def printer(self, value: Printer):
208+
"""Set the printer function."""
209+
if not callable(value):
210+
raise ValueError("printer must be a callable function")
211+
self._printer = value
212+
213+
@property
214+
def res(self) -> int:
215+
"""Get the default number of decimal places to round to."""
216+
return self._res
217+
218+
@res.setter
219+
def res(self, value: int):
220+
"""Set the default number of decimal places to round to."""
221+
if not isinstance(value, int) or value < 0:
222+
raise ValueError("res must be a non-negative integer")
223+
self._res = value
224+
225+
@property
226+
def log(self) -> bool:
227+
"""Get the default log setting."""
228+
return self._log
229+
230+
@log.setter
231+
def log(self, value: bool):
232+
"""Set the default log setting."""
233+
if not isinstance(value, bool):
234+
raise ValueError("log must be a boolean value")
235+
self._log = value
236+
201237
def reset(self):
202238
"""Reset the durations."""
203239
self.durations: dict[str, list[float]] = defaultdict(list)

tests/test_chrono.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_summary():
5252
time.sleep(0.2)
5353

5454
out = []
55-
stopwatch.summary(printer=out.append, res=1)
55+
timer.summary(printer=out.append, res=1)
5656
assert out[0] == "test took 0.2 ± 0.1s (n=2 -> total=0.4s)"
5757
assert out[1] == "test2 took 0.2s"
5858

0 commit comments

Comments
 (0)