Repository navigation
Expand file tree
/
Copy pathcore.py
More file actions
49 lines (43 loc) · 1.72 KB
/
Copy pathcore.py
File metadata and controls
49 lines (43 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import functools
import itertools
import time
from typing import Any, Callable, Iterable, TypeVar
T = TypeVar('T')
def compose(*functions: Callable[[Any], Any]) -> Callable[[Any], Any]:
return functools.reduce(lambda f, g: lambda x: f(g(x)), functions)
def flatten(nested: Iterable[Any]) -> Iterable[Any]:
for item in nested:
if isinstance(item, (list, tuple)):
yield from flatten(item)
else:
yield item
def throttle(seconds: float) -> Callable[[Callable[..., T]], Callable[..., T]]:
def decorator(func: Callable[..., T]) -> Callable[..., T]:
last_called = [0.0]
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> T:
elapsed = time.time() - last_called[0]
if elapsed < seconds:
time.sleep(seconds - elapsed)
result = func(*args, **kwargs)
last_called[0] = time.time()
return result
return wrapper
return decorator
def batch(iterable: Iterable[T], n: int) -> Iterable[tuple[T, ...]]:
args = [iter(iterable)] * n
return zip(*args, strict=False)
def memoize_with_expiry(ttl: int) -> Callable[[Callable[..., T]], Callable[..., T]]:
def decorator(func: Callable[..., T]) -> Callable[..., T]:
cache: dict[tuple[Any, ...], tuple[float, T]] = {}
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> T:
key = (args, tuple(sorted(kwargs.items())))
now = time.time()
if key in cache and now - cache[key][0] < ttl:
return cache[key][1]
result = func(*args, **kwargs)
cache[key] = (now, result)
return result
return wrapper
return decorator