def is_cache_token_left(token_id: str, token_max_cnt: int, token_duration: timezone.timedelta) -> bool: token = cache.get(token_id) if token is None: token_cnt = token_max_cnt token_cooldown = timezone.now() + token_duration else: token_cnt, token_cooldown = token if token_cooldown < timezone.now(): token_cnt = token_max_cnt token_cooldown = timezone.now() + token_duration if token_cnt <= 0: return False token_cnt -= 1 cache.set(token_id, (token_cnt, token_cooldown), token_duration.total_seconds()) return True
def days_left(timedelta: timezone.timedelta): return int(timedelta.total_seconds() // (60 * 60 * 24))
def timedelta_display(time: timezone.timedelta): seconds = time.total_seconds() m, s = divmod(seconds, 60) h, m = divmod(m, 60) return "{:02d}:{:02d}:{:02d}".format(math.floor(h), math.floor(m), math.floor(s))
def _get_project_hours_sum_and_percentage( project_hours: timezone.timedelta, all_hours: timezone.timedelta ) -> Union[Tuple[timezone.timedelta, float], int]: return (project_hours, (project_hours / all_hours) * 100) if all_hours.total_seconds() > 0 else 0