-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspout.py
executable file
·68 lines (58 loc) · 1.9 KB
/
spout.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
#!/bin/python3
import time
# colors
_blue = '\033[38;5;39m'
_yellow = '\033[38;5;226m'
_white = '\033[m'
class ProgressBar():
def __init__(self, width=30, pad=30, color=True):
self.label = str()
self.stages = 0
self._progress = 0
self._width = width
self._pad = pad
self._start = time.perf_counter()
self._current = self._start
self._color = color
def begin(self, label, stages):
# reset if past usage was incomplete
if self._progress:
self.end()
self.label = label
self.stages = stages
print(self, end='\r')
def checkpoint(self):
assert(self.stages > 0)
assert(self._progress < self.stages)
self._current = time.perf_counter()
self._progress += 1
print(self, end="\r" if self._progress < self.stages else '')
def end(self):
self.label = str()
self.stages = 0
self._progress = 0
self._start = time.perf_counter()
self._current = self._start
print()
def _get_label(self):
if self._color:
return _yellow + self.label + _white
return self.label
def _get_bar(self, size):
if self._color:
return _blue + '#'*size + _white
return '#'*size
def __str__(self):
# determine chunk size
length = self._width // self.stages
if self._width % self.stages != 0:
length += 1
# determine final size for bar
size = min(self._progress * length, self._width)
prefix = self._get_label() + '.' * (self._pad - len(self.label))
used = self._get_bar(size)
free = ' ' * (self._width - size)
elapsed = round(self._current - self._start, 2)
return f"{prefix}[{used}{free}] {elapsed:.2f}s"
if __name__ == '__main__':
pass