diff options
| author | Karan Jayachandra <mail@karanjayachandra.com> | 2024-10-17 09:05:12 +0000 |
|---|---|---|
| committer | Karan Jayachandra <mail@karanjayachandra.com> | 2024-10-17 09:05:12 +0000 |
| commit | 299d89197fa4edad742b0ca1838a7b98242b13ce (patch) | |
| tree | 2dc84648312607378ec5ef7d087c578907bb8dc5 /utilities.py | |
| parent | 36cc7d82a55cbd8167e1f52637dd1938497dd8cc (diff) | |
| parent | 02f7ee71560ade362afd415d0664d9243fe017a5 (diff) | |
Merge branch 'feature/docs' into 'develop'
Added a starting point for all documentation
See merge request KaranJayachandra/match_up!4
Diffstat (limited to 'utilities.py')
| -rw-r--r-- | utilities.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/utilities.py b/utilities.py index 1ad24b8..7ba140e 100644 --- a/utilities.py +++ b/utilities.py @@ -1,5 +1,9 @@ +from time import time from typing import Tuple from random import shuffle +from dataclasses import dataclass + +SECS_IN_MIN = 60 def _shuffle_two_lists(a: list, b: list) -> Tuple[list, list]: @@ -21,3 +25,33 @@ def sample_list(indices: list, cost: list[int], count: int = None) -> list: indices, cost = _shuffle_two_lists(indices, cost) indices = _pick_from_list_after_sorting_other(indices, cost)[:count] return indices + + +@dataclass +class Timer: + ref: float = None + running: bool = False + max_mins: int = 20 + default: str = ("00", "00") + + def __post_init__(self): + self.max_seconds = self.max_mins * SECS_IN_MIN + + def start(self): + self.ref = time() + self.max_seconds + self.running = True + + def stop(self): + self.ref = None + self.running = False + + def get(self): + if not self.running: + return self.default + total_seconds = self.ref - time() + if total_seconds < 0: + self.stop() + return self.default + mins = f"{int(total_seconds // SECS_IN_MIN):02d}" + secs = f"{int(total_seconds % SECS_IN_MIN):02d}" + return mins, secs |
