diff options
| author | Karan Jayachandra <mail@karanjayachandra.com> | 2024-11-15 17:26:47 +0000 |
|---|---|---|
| committer | Karan Jayachandra <mail@karanjayachandra.com> | 2024-11-15 17:26:47 +0000 |
| commit | 7b7f8cdef5f035ccdab714059c1a6c54b80c4ba3 (patch) | |
| tree | 2d7ea5de37f2bda57be419b4aa37971afa09eab5 /utilities.py | |
| parent | f68c54ae8f921c23851680b2501fac8bda1b4010 (diff) | |
| parent | 7b8bbe6db3680af65c3701afbcd31ac1f61773af (diff) | |
Merge branch 'develop' into 'main'2.0
Database and javascript timer implemented
See merge request KaranJayachandra/match_up!11
Diffstat (limited to 'utilities.py')
| -rw-r--r-- | utilities.py | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/utilities.py b/utilities.py deleted file mode 100644 index 7ba140e..0000000 --- a/utilities.py +++ /dev/null @@ -1,57 +0,0 @@ -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]: - indices = list(range(len(a))) - shuffle(indices) - a = [a[index] for index in indices] - b = [b[index] for index in indices] - return a, b - - -def _pick_from_list_after_sorting_other(a: list, b: list[int]) -> list: - indices = [i[0] for i in sorted(enumerate(b), key=lambda x: x[1])] - return [a[index] for index in indices] - - -def sample_list(indices: list, cost: list[int], count: int = None) -> list: - if count is None: - count = len(indices) - 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 |
