An error occurred while loading the file. Please try again.
-
Pierre-Antoine Rouby authorede7b6e668
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
import time
from functools import (
reduce, partial, wraps
)
##########
# TIMERS #
##########
_timers = {}
_calls = {}
def reset_timers():
global _timers
global _calls
_timers = {}
_calls = {}
def display_timers():
global _timers
global _calls
print(" +---------------------------------------------------------Timers--+")
lst = sorted(
map(
lambda f: (f, _timers[f], _calls[f]),
_timers
),
key = lambda f: f[1],
reverse = True
)
for func, time, calls in lst:
print(f" | {func:<32} | {time:>10.6f} sec | {calls:>5} calls |")
print(" +-----------------------------------------------------------------+")
def timer(func):
"""Function wrapper to register function runtime"""
@wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter()
value = None
try:
value = func(*args, **kwargs)
except Exception as e:
print(f"{e}")
end_time = time.perf_counter()
run_time = end_time - start_time
if func.__qualname__ not in _timers:
_timers[func.__qualname__] = 0
_calls[func.__qualname__] = 0
_timers[func.__qualname__] += run_time
_calls[func.__qualname__] += 1
return value
return wrapper
################
# OTHERS TOOLS #
################
@timer
def flatten(lst):
"""Flatten list of list
Args:
lst: A list of list
Returns:
returns a list of element
"""
if not lst:
return []
return reduce(list.__add__, lst)