-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogressbar.py
More file actions
126 lines (106 loc) · 4.44 KB
/
Copy pathprogressbar.py
File metadata and controls
126 lines (106 loc) · 4.44 KB
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
import sys
import time
from datetime import timedelta
class ProgressBar:
"""
Instantiate a progress bar. It assumes values from 0 to end_value - 1.
Usage:
>>> bar = ProgressBar(end_value=100, text="Iteration")
>>> bar.start()
>>> for i in range(0, 100):
>>> time.sleep(1)
>>> bar.update(i)
"""
def __init__(self, end_value=100, text=None, count=False, bar_length=20):
"""
Initialize the progress bar.
:param end_value: Maximum number of iterations
:param text: Text to be displayed before the bar
:param count: Whether to display the count of iterations
:param bar_length: Number of hashes to display
"""
self.end_value = end_value
self.current = 0
self.bar_length = bar_length
self.start_time = 0
self.count = count
if text:
self.text = text
else:
self.text = "Progress"
def _reset(self):
"""
Reset the bar.
"""
self.current = 0
self.start_time = 0
@staticmethod
def _print(text):
"""
Print text to the console.
:param text: Text to be printed
"""
sys.stdout.write(text)
sys.stdout.flush()
def start(self, status_text=None):
"""
Start the timer and displaying the bar.
:param status_text: Optional status text to be displayed after the bar
"""
self._reset()
# Initialize starting time
self.start_time = int(time.time())
# Start displaying the bar
if self.count:
display_text = "\r{0} ({4:>{5}}/{3}): [{1}] {2}%".format(self.text,
' ' * self.bar_length,
0,
self.end_value,
0,
len(str(self.end_value)))
else:
display_text = "\r{0}: [{1}] {2}%".format(self.text,
' ' * self.bar_length,
self.current)
if status_text is not None:
display_text += " %s" % status_text
self._print(display_text)
def current_status(self, status_text):
"""
Display the current status as a text after the bar
:param status_text: Status text to be displayed
"""
self._print(" " + status_text)
def update(self, new_val):
"""
Update the bar with new_val.
:param new_val: New current value
"""
self.current = new_val + 1
if self.current <= self.end_value:
percent = float(self.current) / self.end_value
hashes = '#' * int(round(percent * self.bar_length))
spaces = ' ' * (self.bar_length - len(hashes))
if self.count:
display_text = "\r{0} ({4:>{5}}/{3}): [{1}] {2}%".format(self.text,
hashes + spaces,
int(round(percent * 100)),
self.end_value,
self.current,
len(str(self.end_value)))
else:
display_text = "\r{0}: [{1}] {2}%".format(self.text,
hashes + spaces,
int(round(percent * 100)))
if self.current == self.end_value:
elapsed_time = timedelta(seconds=(int(time.time()) - self.start_time))
display_text += " Elapsed time: %s\n" % elapsed_time
self._print(display_text)
def progress_bar_test(end_value, text, bar_length, count):
bar = ProgressBar(end_value=end_value, text=text, count=count, bar_length=bar_length)
bar.start()
for i in range(0, end_value):
time.sleep(1)
bar.update(i)
if __name__ == '__main__':
progress_bar_test(100, text='Iterations', count=True, bar_length=20)