X-Git-Url: https://plomlompom.com/repos/day?a=blobdiff_plain;f=plomtask%2Fmisc.py;h=ab39df098059e416c7627b5fcefecae2668d7c06;hb=bdc1f2e1962b745d72c6a94f5071c24455c54b18;hp=5759c0d8b6bcc5915f84b2e94a533eefa70954e7;hpb=7adcf651f0053f0dc5d719457a016a0d5b12253b;p=plomtask diff --git a/plomtask/misc.py b/plomtask/misc.py index 5759c0d..ab39df0 100644 --- a/plomtask/misc.py +++ b/plomtask/misc.py @@ -2,8 +2,11 @@ from datetime import datetime from typing import Any from sqlite3 import Row +from time import sleep from plomtask.db import DatabaseConnection +TIMESTAMP_FMT = '%Y-%m-%d %H:%M:%S.%f' + class VersionedAttribute: """Attributes whose values are recorded as a timestamped history.""" @@ -28,10 +31,19 @@ class VersionedAttribute: return self.history[self._newest_timestamp] def set(self, value: str | float) -> None: - """Add to self.history if and only if not same value as newest one.""" + """Add to self.history if and only if not same value as newest one. + + Note that we wait one micro-second, as timestamp comparison to check + most recent elements only goes up to that precision. + + Also note that we don't check against .newest because that may make us + compare value against .default even if not set. We want to be able to + explicitly set .default as the first element. + """ + sleep(0.00001) if 0 == len(self.history) \ or value != self.history[self._newest_timestamp]: - self.history[datetime.now().strftime('%Y-%m-%d %H:%M:%S')] = value + self.history[datetime.now().strftime(TIMESTAMP_FMT)] = value def history_from_row(self, row: Row) -> None: """Extend self.history from expected table row format."""