-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBar.py
65 lines (59 loc) · 2.14 KB
/
Bar.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
"""
This class prints a visual guidance of the progress of a task
"""
import math
import time
class Bar:
"""
This class prints a visual guidance of the progress of a task
"""
def __init__(self, limit, text, initial=0, increment=1):
""" Creates the bar, and shows it. """
self.limit = limit
self.value = initial
self.increment = increment
self.points = []
for _ in range(0, min(int(self.limit), 100)):
self.points.append("░")
self.current_point = 0
self.starting_time = time.time()
self.info = text
self.update()
def progress(self):
""" Progress the bar by the increment defined. """
self.value += self.increment
percentage = (min(self.limit, 100) * self.value)/self.limit
point = percentage - self.current_point
if point >= 1:
self.points[self.current_point % min(self.limit, 100)] = "█"
self.current_point += 1
self.update()
def update(self):
""" Visually updates the bar. """
out = "{}".format("".join(self.points))
rate = " {}/{} ".format(self.current_point, min(self.limit, 100))
elapsed = time.time() - self.starting_time
elapsed = self.format_time(elapsed)
elapsed = " [{}]".format(elapsed)
print("\r{:<25} {}{}{}".format(self.info, out, rate, elapsed), end="")
def format_time(self, timestamp):
""" Format the time from timestamp to hh:mm:ss. """
hour = 0
minute = 0
seconds = 0
if timestamp > 3600:
while timestamp >= 3600:
hour += 1
timestamp -= 3600
if timestamp > 60:
while timestamp >= 60:
minute += 1
timestamp -= 60
seconds = math.floor(timestamp)
hour = "0" + str(hour) if hour < 10 else hour
minute = "0" + str(minute) if minute < 10 else minute
seconds = "0" + str(seconds) if seconds < 10 else seconds
return "{}:{}:{}".format(hour, minute, seconds)
def finish(self):
""" Outputs a clear line. """
print()