Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (49 loc) · 1.97 KB
/
Copy pathutils.py
File metadata and controls
64 lines (49 loc) · 1.97 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import time
from functools import wraps
from typing import Any, Callable, Dict, Generator, Iterable, List, TypeVar
T = TypeVar("T")
def timed_lru_cache(seconds: int, maxsize: int = 128) -> Callable:
"""Decorator providing an LRU cache with time-based expiration (TTL)."""
def decorator(func: Callable) -> Callable:
cache: Dict[tuple, tuple[Any, float]] = {}
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
key = (args, tuple(sorted(kwargs.items())))
now = time.monotonic()
if key in cache:
result, timestamp = cache[key]
if now - timestamp < seconds:
return result
result = func(*args, **kwargs)
if len(cache) >= maxsize and key not in cache:
oldest_key = next(iter(cache))
del cache[oldest_key]
cache[key] = (result, now)
return result
return wrapper
return decorator
def chunk_iterable(iterable: Iterable[T], size: int) -> Generator[List[T], None, None]:
"""Efficiently split an iterable into chunks of specified size using memory-friendly yield."""
if size <= 0:
raise ValueError("Chunk size must be greater than 0")
chunk: List[T] = []
for item in iterable:
chunk.append(item)
if len(chunk) == size:
yield chunk
chunk = []
if chunk:
yield chunk
def fast_flatten_dict(d: Dict[str, Any], parent_key: str = "", sep: str = ".") -> Dict[str, Any]:
"""Iterative, memory-optimized dictionary flattening to reduce call stack overhead."""
items: Dict[str, Any] = {}
stack = [(d, parent_key)]
while stack:
curr, prefix = stack.pop()
for k, v in curr.items():
new_key = f"{prefix}{sep}{k}" if prefix else str(k)
if isinstance(v, dict) and v:
stack.append((v, new_key))
else:
items[new_key] = v
return items