Skip to content

Instantly share code, notes, and snippets.

@biblicabeebli
biblicabeebli / timezones.py
Created Feb 17, 2021
The proper way to get a list of timezones
View timezones.py
from collections import defaultdict
from datetime import timedelta
import pytz
from dateutil import tz
def timedelta_to_label(td: timedelta) -> str:
""" returns a string like +1:00 """
label = "-" + str(abs(td)) if td.total_seconds() < 0 else "+" + str(abs(td))
return label[:-3]
@biblicabeebli
biblicabeebli / timeout_cache.py
Created Feb 17, 2021
An elegant little timeout cache decorator for python
View timeout_cache.py
from functools import wraps
from time import perf_counter # perf_counter is a highly quality timestamp
# Keys are the functions themselves, values are a tuple containing the timeout expiry
# of the cache entry, and the cached return value.
function_cache = {}
def timeout_cache(seconds: int or float):
def check_timeout_cache(wrapped_func):