···
from typing import Callable, Optional
2
+
from math import isfinite
from .logger import rootlog
···
description: Optional[str]
···
self.description = str(description)
self.last_called = float("-inf")
37
-
self.entered = False
38
+
self.entry_count = 0
39
-
def check(self) -> bool:
40
-
if self.entered or not self.overdue:
40
+
def check(self, force: bool = False) -> bool:
41
+
if (self.entered or not self.overdue) and not force:
with self, rootlog.nested(self.nested_message):
44
-
rootlog.info(f"Time since last: {time.monotonic() - self.last_called:.2f}s")
45
+
time_since_last = time.monotonic() - self.last_called
47
+
f"Time since last: {time_since_last:.2f}s"
48
+
if isfinite(time_since_last)
49
+
else "(not called yet)"
52
+
rootlog.info(last_message)
res = self.condition() # type: ignore
···
def overdue(self) -> bool:
return self.last_called + self.seconds_interval < time.monotonic()
81
+
def entered(self) -> bool:
82
+
# entry_count should never dip *below* zero
83
+
assert self.entry_count >= 0
84
+
return self.entry_count > 0
def __enter__(self) -> None:
87
+
self.entry_count += 1
def __exit__(self, exc_type, exc_value, traceback) -> None: # type: ignore
76
-
self.entered = False
91
+
self.entry_count -= 1
self.last_called = time.monotonic()