File size: 766 Bytes
80b93ff
e190970
 
80b93ff
 
 
 
 
 
 
 
 
 
 
 
 
e190970
 
 
 
 
 
 
80b93ff
 
 
e190970
80b93ff
 
 
 
e190970
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
from functools import wraps
from time import time


def simple_time_tracker(log_fun):
    def _simple_time_tracker(fn):
        @wraps(fn)
        def wrapped_fn(*args, **kwargs):
            start_time = time()

            try:
                result = fn(*args, **kwargs)
            finally:
                elapsed_time = time() - start_time

                # log the result
                log_fun(
                    {
                        "function_name": fn.__name__,
                        "total_time": elapsed_time,
                    }
                )

            return result

        return wrapped_fn

    return _simple_time_tracker


def _log(message):
    print("[SimpleTimeTracker] {function_name} {total_time:.3f}".format(**message))