-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
44 lines (37 loc) · 1.45 KB
/
utils.py
File metadata and controls
44 lines (37 loc) · 1.45 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
from typing import Callable, TypeVar, Any
from collections.abc import Sequence
import re
def find_occurences(pattern:str, line:str, overlap:bool=False):
"""Find occurences of str in line."""
indices:list[int] = []
regex = re.compile(pattern)
if overlap:
regex = re.compile(f'(?=({pattern}))')
for match in regex.finditer(line):
indices.append(match.start())
return indices
I = TypeVar('I')
S = TypeVar('S', bound=Sequence[I])
def chunk_list(lst: Sequence[I], size: int) -> list[S]:
"""Chunk a list into smaller lists of given size."""
return [list(lst[i:i + size]) for i in range(0, len(lst), size)]
T = TypeVar('T', bound=Callable[..., Any])
def perf_timer(func: T) -> T:
"""Decorator to time function performance."""
import time
def wrapper(*args: Any, **kwargs: Any) -> Any:
start = time.time()
result = func(*args, **kwargs)
end = time.time()
duration = end - start
if duration < 0.001:
print(f'{func.__name__} took {duration * 1000000:.2f} microseconds')
elif duration < 1:
print(f'{func.__name__} took {duration * 1000:.2f} milliseconds')
else:
print(f'{func.__name__} took {duration:.2f} seconds')
return result
return wrapper
def manhattan(a: Sequence[int], b: Sequence[int]) -> int:
"""Calculate the Manhattan distance between two point/vectors."""
return sum(abs(x - y) for x, y in zip(a, b))