-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.py
68 lines (60 loc) · 2.7 KB
/
runtime.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
import timeit
class RuntimeEstimate:
def __init__(self):
self.start_time = None
self.total_time = None
self.last_time = None
self.progress = None
# Start the timer and reset progress
def start(self):
self.start_time = timeit.default_timer()
self.last_time = self.start_time
self.progress = 0
# Returns how much time has elapsed in seconds since the timer started
def elapsed_time(self):
if self.start_time is None:
raise Exception("Timer not started!")
return timeit.default_timer() - self.start_time
# Returns how much time the whole process should take from start to finish
def estimated_total_time(self):
if self.start_time is None:
raise Exception("Timer not started!")
if self.progress is None or self.progress == 0:
raise Exception("No progress information available!")
return self.elapsed_time() / self.progress
# Returns an estimate of how much time is left before the process is complete
def estimated_remaining_time(self):
if self.start_time is None:
raise Exception("Timer not started!")
if self.progress is None or self.progress == 0:
raise Exception("No progress information available!")
return self.estimated_total_time() * (1 - self.progress)
# Update with the current amount of progress expressed as a fraction from 0 (not started) to 1 (finished)
def update_progress(self, progress):
self.progress = progress
now = timeit.default_timer()
self.total_time = now - self.start_time
self.last_time = now
def __str__(self):
if self.start_time is None:
raise Exception("Timer not started!")
if self.progress is None:
return "Elapsed time: {}".format(self.format_time(self.elapsed_time()))
else:
return "Elapsed time: {}; Estimated remaining time: {}".format(self.format_time(self.elapsed_time()), self.format_time(self.estimated_remaining_time()))
@staticmethod
def format_time(seconds):
if seconds < 60:
return "{:.2f} seconds".format(seconds)
elif seconds < 3600:
return "{:.2f} minutes".format(seconds / 60)
elif seconds < 86400:
return "{:.2f} hours".format(seconds / 3600)
elif seconds < 604800:
return "{:.2f} days".format(seconds / 86400)
elif seconds < 2592000:
return "{:.2f} weeks".format(seconds / 604800)
elif seconds < 31536000:
return "{:.2f} months".format(seconds / 2592000)
else:
return "{:.2f} years".format(seconds / 31536000)