-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.py
38 lines (30 loc) · 1.29 KB
/
lib.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
from functional import seq
from functools import wraps
import sys
import time
class logtime:
@classmethod
def _log_msg(cls, f, elapsed_time, *args, **kwargs):
arg_string = (seq(args).map(repr) +
seq(kwargs.items()).map(
lambda kv: '{!r}={!r}'.format(kv[0], kv[1]))
).make_string(', ')
log_msg = "{}({}) - execution time: {:.2f} ".format(f.__name__, arg_string, elapsed_time)
return log_msg
def __init__(self, logger=None, handler=None):
if logger:
self.handler = lambda f, elapsed_time, *args, **kwargs: \
logger.info(logtime._log_msg(f, elapsed_time, *args, **kwargs))
elif handler:
self.handler = lambda f, elapsed_time, *args, **kwargs: handler(f, elapsed_time, *args, **kwargs)
else:
self.handler = lambda f, elapsed_time, *args, **kwargs: \
print(logtime._log_msg(f, elapsed_time, *args, **kwargs), file=sys.stderr)
def __call__(self, f):
def wrapped(*args, **kwargs):
start = time.time()
out = f(*args, **kwargs)
end = time.time()
self.handler(f, end-start, *args, **kwargs)
return out
return wrapped