-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
43 lines (34 loc) · 1.09 KB
/
utils.py
File metadata and controls
43 lines (34 loc) · 1.09 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
from functools import wraps
import time
import logging
import inspect
import asyncio
logger = logging.getLogger('deepcode')
def coro(f):
@wraps(f)
def wrapper(*args, **kwargs):
loop = asyncio.get_event_loop()
loop.run_until_complete(f(*args, **kwargs))
#return asyncio.run() # supported only from 3.7+
return wrapper
def profile_speed(func):
log_timing = lambda d: logger.debug("- {:6.2f} sec: Done - \"{}\"".format(
d, func.__doc__ or func.__name__))
if inspect.iscoroutinefunction(func):
@wraps(func)
async def wrapper(*args, **kwargs):
start_time = time.time()
try:
return await func(*args, **kwargs)
finally:
log_timing(time.time() - start_time)
return wrapper
else:
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
try:
return func(*args, **kwargs)
finally:
log_timing(time.time() - start_time)
return wrapper