-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
057427f
commit 134ba44
Showing
3 changed files
with
51 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import annotations | ||
|
||
import time | ||
|
||
|
||
class Deadline: | ||
def __init__(self, *, timestamp: float): | ||
self._timestamp = timestamp | ||
|
||
def __lt__(self, other: object) -> bool: | ||
if not isinstance(other, Deadline): | ||
raise TypeError( | ||
f"Comparison is not supported between {type(self).__name__} and {type(other).__name__}" | ||
) | ||
|
||
return self._timestamp < other._timestamp | ||
|
||
def __eq__(self, other: object) -> bool: | ||
if not isinstance(other, Deadline): | ||
raise TypeError( | ||
f"Comparison is not supported between {type(self).__name__} and {type(other).__name__}" | ||
) | ||
|
||
return self._timestamp == other._timestamp | ||
|
||
@classmethod | ||
def from_timeout(cls, timeout: float) -> Deadline: | ||
return cls(timestamp=time.monotonic() + timeout) | ||
|
||
def time_remaining(self) -> float: | ||
return max(0, self._timestamp - time.monotonic()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters