-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
54 lines (45 loc) · 1.51 KB
/
utils.py
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
import time
import traceback
from contextlib import contextmanager
from typing import Callable, TypeVar, Generator
@contextmanager
def ignored(*errors, msg: str = "", show_traceback: bool = False, callback: Callable[[Exception], None] = lambda _: ...) -> Generator:
"""
Ignores the specified errors for the duration of the context manager block.
:param errors: The errors to ignore.
:param msg: If specified, this message is printed to console when an error is caught.
:param show_traceback: If true, prints the error traceback.
:param callback: this function is run if an error occurs
"""
try:
yield
except errors as e:
if msg:
print(msg)
if show_traceback:
print(traceback.format_exc())
callback(e)
exec_time = {}
@contextmanager
def timed(name: str = "", /, *, print_time: bool = True) -> Generator:
"""
Records the execution time of the context manager block.
:param name: If specified, the execution time will be recorded in the exec_times dict with this name as the key
:param print_time: If true, print the context manager execution time to console
"""
global exec_time
start = time.perf_counter_ns()
yield
end = time.perf_counter_ns()
runtime = (end-start)/10**6
if name:
exec_time[name] = runtime
if print_time:
print(name, "took:", runtime, "ms")
T = TypeVar("T")
def log(v: T, tag: str = "") -> T:
if tag:
print(tag, v)
return v
print(v)
return v