-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_stamp.py
149 lines (114 loc) · 4.16 KB
/
time_stamp.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import time
from enum import Enum
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# GLOBAL VARIABLES #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
start_time = 0.0
previous = {}
laps = {}
text_size_limit = 32
round_time = 5
separator = "|"
buffer_limit = 100
time_measure_enabled = True
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# TIME STAMP FUCTIONS #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Adds a new lap to the buffer
def add_lap(lap, id):
if time_measure_enabled :
global laps
if(id in laps):
laps[id].append(lap)
pass
else:
laps[id] = [lap]
pass
# Erases all saved values and considers curent time as entry of program
# Argument enables to reset the laps without modifying the starting time
def reset(all_vars=False):
if time_measure_enabled :
global laps
global previous
global start_time
laps = {}
previous = {}
if(all_vars):
start_time = time.time()
# Measures the time ellaŝed between two calls of this function, with 'txt' as log-line
# Automatic saving in a list
def start_function(id):
if time_measure_enabled :
previous[id] = time.time()
def tour(txt, id):
if time_measure_enabled :
global previous
if(len(txt) > text_size_limit):
print("Warning: Text too long")
txt = txt[0:text_size_limit]
missing = text_size_limit - len(txt)
missing = " " * missing
if(id in previous): # Deja init dans cette fonction
elapsed = time.time() - previous[id]
elapsed = round(elapsed, round_time)
add_lap([(txt + missing + separator + " "), elapsed], id)
previous[id] = time.time()
pass
else:
elapsed = 0.0
add_lap([(txt + missing + separator + " "), elapsed], id)
previous[id] = time.time()
pass
# Displays all
def disp_laps():
if time_measure_enabled :
for key in laps.keys():
print(key)
for lap in laps[key]:
print(lap[0] + str(lap[1]))
print("\n")
# Writes the saved laps in the file specified as parameter
def write_laps(file, write_all=True):
if time_measure_enabled :
f = open(file, "w")
for key in laps.keys():
f.write(" ### " + str(key) + " ###\n")
for lap in laps[key]:
f.write(lap[0] + str(lap[1]) + "\n")
f.write("\n\n")
if(write_all):
s = ("\n\nTOTAL:\n %s seconds " % (time.time() - start_time))
f.write(s)
f.close()
# Returns the total time of execution
def exec_time():
if time_measure_enabled :
global start_time
return time.time() - start_time
# Displays and returns the total time of execution
def disp_exec_time():
print("--- %s seconds ---" % (time.time() - start_time))
class TimeSort(Enum):
DEFAULT = 0
LONGER = 1
SHORTER = 2
def sort_laps(sorting=TimeSort.DEFAULT):
if time_measure_enabled :
global laps
for cle in laps.keys():
if(sorting == TimeSort.DEFAULT):
pass
elif(sorting == TimeSort.LONGER):
laps[cle] = sorted(laps[cle], key=lambda tup: tup[1], reverse=True)
pass
elif(sorting == TimeSort.SHORTER):
laps[cle] = sorted(laps[cle], key=lambda tup: tup[1])
pass
else:
print("Warning: Unknown sorting parameter. No sorting applied.")
pass
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# TIME STAMP FUCTIONS #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
start_time = time.time()
previous = {}