-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.py
174 lines (125 loc) · 3.63 KB
/
examples.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from datetime import timedelta
import sys
import time
try:
import psutil
except ImportError:
psutil = None
from progressist import ProgressBar
def loop():
for i in range(20):
time.sleep(0.2)
yield True
def call(bar):
for x in bar.iter(loop()):
continue
REGISTRY = []
def register(func):
REGISTRY.append(func)
return func
@register
def example_default():
bar = ProgressBar(total=20, prefix="Default:")
call(bar)
@register
def example_custom_done_char():
bar = ProgressBar(total=20, done_char="█", prefix="Custom fill character:")
call(bar)
@register
def example_custom_remain_char():
"""Custom empty fill character."""
bar = ProgressBar(
total=20, done_char="◉", remain_char="◯", prefix="Custom empty fill char:"
)
call(bar)
@register
def example_with_eta():
bar = ProgressBar(total=20, template="With ETA: {animation} ETA: {eta}")
call(bar)
@register
def example_with_avg():
bar = ProgressBar(total=20, template="With Average: {animation} Avg: {avg} s/item")
call(bar)
@register
def example_with_custom_color():
bar = ProgressBar(
total=20,
remain_char="-",
invisible_chars=11,
template="\r\033[34mCustom color: {animation}\033[39m",
)
call(bar)
@register
def example_with_custom_class():
class MyBar(ProgressBar):
@property
def swap(self):
if not psutil:
return "psutil not installed"
return psutil.swap_memory().total
bar = MyBar(
total=20, template="Custom class with custom info: " "{animation} Swap: {swap}"
)
call(bar)
@register
def example_stream():
bar = ProgressBar(
total=20,
animation="{stream}",
steps=["⎺", "⎻", "⎼", "⎽", "⎼", "⎻"],
template="Stream {animation} {elapsed}",
)
call(bar)
@register
def example_throttle():
bar = ProgressBar(total=20, throttle=2, prefix="Throttling")
call(bar)
@register
def example_throttle_by_second():
bar = ProgressBar(
total=20, throttle=timedelta(seconds=1), prefix="Throttling by second"
)
call(bar)
@register
def example_spinner():
bar = ProgressBar(total=20, prefix="Spinner", animation="{spinner}")
call(bar)
@register
def example_spinner_without_total():
bar = ProgressBar(
animation="{spinner}",
steps=["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"],
template="Spinner without total: {animation} " "Elapsed: {elapsed}",
)
call(bar)
@register
def example_reverse_bar():
class MyBar(ProgressBar):
@property
def progress(self):
done_chars = int(self.fraction * self.free_space)
remain_chars = self.free_space - done_chars
return self.remain_char * remain_chars + self.done_char * done_chars
bar = MyBar(total=20, template="{percent} {animation} Reverse bar")
call(bar)
@register
def example_download():
class DownloadBar(ProgressBar):
"""Example of a resuming download, displaying human readable sizes."""
template = "Download |{animation}| {done:B}/{total:B} ({speed:B}/s)"
done_char = "█"
bar = DownloadBar()
for i in range(82944500, 197739688 + 165889, 165889):
time.sleep(0.002)
bar.update(done=i, total=197739688)
if __name__ == "__main__":
if len(sys.argv) == 2:
for func in REGISTRY:
if func.__name__ == sys.argv[1]:
func()
break
else:
print("No func found with name", sys.argv[1])
else:
for func in REGISTRY:
func()