-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrappers.py
More file actions
57 lines (47 loc) · 1.23 KB
/
Copy pathwrappers.py
File metadata and controls
57 lines (47 loc) · 1.23 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
import time
from datetime import datetime, timezone
def smart_wrapper(func):
def wrapper(*args, **kwargs):
start = time.time()
try:
result = func(*args, **kwargs) # <- Any args
elapsed = (time.time() - start)*1000
print(f"{func.__name__}() completed in {elapsed:.3f}ms")
return result
except Exception as e:
elapsed = (time.time() - start)*1000
print(f"{func.__name__} failed: {e}")
raise # prevent misleading tracebacks by passing failed function returns
return wrapper
@smart_wrapper
def do_this():
print("Doing this!")
@smart_wrapper
def do_that():
print("Doing that!")
@smart_wrapper
def risky():
try:
x = 1 / 0
except ZeroDivisionError as e:
print("Can't divide by zero, homey. Have a nice shiny 0 instead.")
return 0
@smart_wrapper
def slow_sleep(n):
time.sleep(n)
@smart_wrapper
def fast_math(x, y):
return x ** y
@smart_wrapper
def load_level():
# heavy YAML + GLTF
...
@smart_wrapper
def update_physics():
# collision checks
...
do_that()
do_this()
risky()
slow_sleep(1) # -> slow_sleep(): ~1000ms
fast_math(2, 10) # -> fast_math(): 0