-
Notifications
You must be signed in to change notification settings - Fork 1
/
progress.py
71 lines (56 loc) · 1.58 KB
/
progress.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
import math
import time
class Progress:
def __init__(self, goal, start=True):
self._progress = 0
self._goal = goal
if start:
self.start()
def start(self):
self._progress = 0
self._start_time = time.monotonic()
def advance(self, amount=1):
self._progress += amount
def is_done(self):
return not (self._progress < self._goal)
def get_progress(self):
return self._progress
def get_goal(self):
return self._goal
def get_remaining(self):
return self._goal - self._progress
def get_fraction(self):
if self._goal == 0:
return 1
return self._progress / self._goal
def get_elapsed(self):
return time.monotonic() - self._start_time
def get_progress_per_second(self):
if self.get_elapsed() == 0:
return 0
return self._progress / self.get_elapsed()
def get_eta(self):
pps = self.get_progress_per_second()
if pps == 0:
return float("inf")
return self.get_remaining() / pps
def get_bar(self, width=50, fill="=", edges="|", space=" "):
f = min(round(self.get_fraction() * width), width)
s = min(width - f, width)
l_edge = edges[0] if len(edges) >= 1 else ""
r_edge = edges[1] if len(edges) >= 2 else l_edge
return l_edge + (fill * f) + (space * s) + r_edge
def get_percent(self):
return self.get_fraction() * 100
class ProgressBar(Progress):
def __str__(self):
rate = self.get_progress_per_second()
if rate > 1.0 or rate == 0:
rate_str = "%.3f/s" % (rate,)
else:
rate_str = "%.3fs/" % ((1 / rate),)
bar = "%s%3d%% ETA: %.0fs, %s" % (
self.get_bar(width=40), self.get_percent(),
self.get_eta(), rate_str
)
return bar