Debian Codegraph @

tests/original_examples.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #!/usr/bin/python
2
3 import sys
4 import time
5
•6 from progressbar2 import (
7 ETA,
8 AdaptiveETA,
9 AnimatedMarker,
10 Bar,
11 BouncingBar,
12 Counter,
13 FileTransferSpeed,
14 FormatLabel,
15 Percentage,
16 ProgressBar,
17 ReverseBar,
18 RotatingMarker,
19 SimpleProgress,
20 Timer,
21 UnknownLength,
22 )
23
24 examples = []
25
26
27 def example(fn):
28 try:
29 name = 'Example %d' % int(fn.__name__[7:])
30 except Exception:
31 name = fn.__name__
32
33 def wrapped():
34 try:
35 sys.stdout.write(f'Running: {name}\n')
36 fn()
37 sys.stdout.write('\n')
38 except KeyboardInterrupt:
39 sys.stdout.write('\nSkipping example.\n\n')
40
41 examples.append(wrapped)
42 return wrapped
43
44
45 @example
46 def example0() -> None:
47 pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=300).start()
48 for i in range(300):
49 time.sleep(0.01)
50 pbar.update(i + 1)
51 pbar.finish()
52
53
54 @example
55 def example1() -> None:
56 widgets = [
57 'Test: ',
58 Percentage(),
59 ' ',
60 Bar(marker=RotatingMarker()),
61 ' ',
62 ETA(),
63 ' ',
64 FileTransferSpeed(),
65 ]
66 pbar = ProgressBar(widgets=widgets, maxval=10000).start()
67 for i in range(1000):
68 # do something
69 pbar.update(10 * i + 1)
70 pbar.finish()
71
72
73 @example
74 def example2() -> None:
75 class CrazyFileTransferSpeed(FileTransferSpeed):
76 """It's bigger between 45 and 80 percent."""
77
78 def update(self, pbar):
79 if 45 < pbar.percentage() < 80:
80 return 'Bigger Now ' + FileTransferSpeed.update(self, pbar)
81 else:
82 return FileTransferSpeed.update(self, pbar)
83
84 widgets = [
85 CrazyFileTransferSpeed(),
86 ' <<<',
87 Bar(),
88 '>>> ',
89 Percentage(),
90 ' ',
91 ETA(),
92 ]
93 pbar = ProgressBar(widgets=widgets, maxval=10000)
94 # maybe do something
95 pbar.start()
96 for i in range(2000):
97 # do something
98 pbar.update(5 * i + 1)
99 pbar.finish()
100
101
102 @example
103 def example3() -> None:
104 widgets = [Bar('>'), ' ', ETA(), ' ', ReverseBar('<')]
105 pbar = ProgressBar(widgets=widgets, maxval=10000).start()
106 for i in range(1000):
107 # do something
108 pbar.update(10 * i + 1)
109 pbar.finish()
110
111
112 @example
113 def example4() -> None:
114 widgets = [
115 'Test: ',
116 Percentage(),
117 ' ',
118 Bar(marker='0', left='[', right=']'),
119 ' ',
120 ETA(),
121 ' ',
122 FileTransferSpeed(),
123 ]
124 pbar = ProgressBar(widgets=widgets, maxval=500)
125 pbar.start()
126 for i in range(100, 500 + 1, 50):
127 time.sleep(0.2)
128 pbar.update(i)
129 pbar.finish()
130
131
132 @example
133 def example5() -> None:
134 pbar = ProgressBar(widgets=[SimpleProgress()], maxval=17).start()
135 for i in range(17):
136 time.sleep(0.2)
137 pbar.update(i + 1)
138 pbar.finish()
139
140
141 @example
142 def example6() -> None:
143 pbar = ProgressBar().start()
144 for i in range(100):
145 time.sleep(0.01)
146 pbar.update(i + 1)
147 pbar.finish()
148
149
150 @example
151 def example7() -> None:
152 pbar = ProgressBar() # Progressbar can guess maxval automatically.
153 for _i in pbar(range(80)):
154 time.sleep(0.01)
155
156
157 @example
158 def example8() -> None:
159 pbar = ProgressBar(maxval=80) # Progressbar can't guess maxval.
160 for _i in pbar(i for i in range(80)):
161 time.sleep(0.01)
162
163
164 @example
165 def example9() -> None:
166 pbar = ProgressBar(widgets=['Working: ', AnimatedMarker()])
167 for _i in pbar(i for i in range(50)):
168 time.sleep(0.08)
169
170
171 @example
172 def example10() -> None:
173 widgets = ['Processed: ', Counter(), ' lines (', Timer(), ')']
174 pbar = ProgressBar(widgets=widgets)
175 for _i in pbar(i for i in range(150)):
176 time.sleep(0.1)
177
178
179 @example
180 def example11() -> None:
181 widgets = [FormatLabel('Processed: %(value)d lines (in: %(elapsed)s)')]
182 pbar = ProgressBar(widgets=widgets)
183 for _i in pbar(i for i in range(150)):
184 time.sleep(0.1)
185
186
187 @example
188 def example12() -> None:
189 widgets = ['Balloon: ', AnimatedMarker(markers='.oO@* ')]
190 pbar = ProgressBar(widgets=widgets)
191 for _i in pbar(i for i in range(24)):
192 time.sleep(0.3)
193
194
195 @example
196 def example13() -> None:
197 # You may need python 3.x to see this correctly
198 try:
199 widgets = ['Arrows: ', AnimatedMarker(markers='←↖↑↗→↘↓↙')]
200 pbar = ProgressBar(widgets=widgets)
201 for _i in pbar(i for i in range(24)):
202 time.sleep(0.3)
203 except UnicodeError:
204 sys.stdout.write('Unicode error: skipping example')
205
206
207 @example
208 def example14() -> None:
209 # You may need python 3.x to see this correctly
210 try:
211 widgets = ['Arrows: ', AnimatedMarker(markers='◢◣◤◥')]
212 pbar = ProgressBar(widgets=widgets)
213 for _i in pbar(i for i in range(24)):
214 time.sleep(0.3)
215 except UnicodeError:
216 sys.stdout.write('Unicode error: skipping example')
217
218
219 @example
220 def example15() -> None:
221 # You may need python 3.x to see this correctly
222 try:
223 widgets = ['Wheels: ', AnimatedMarker(markers='◐◓◑◒')]
224 pbar = ProgressBar(widgets=widgets)
225 for _i in pbar(i for i in range(24)):
226 time.sleep(0.3)
227 except UnicodeError:
228 sys.stdout.write('Unicode error: skipping example')
229
230
231 @example
232 def example16() -> None:
233 widgets = [FormatLabel('Bouncer: value %(value)d - '), BouncingBar()]
234 pbar = ProgressBar(widgets=widgets)
235 for _i in pbar(i for i in range(180)):
236 time.sleep(0.05)
237
238
239 @example
240 def example17() -> None:
241 widgets = [
242 FormatLabel('Animated Bouncer: value %(value)d - '),
243 BouncingBar(marker=RotatingMarker()),
244 ]
245
246 pbar = ProgressBar(widgets=widgets)
247 for _i in pbar(i for i in range(180)):
248 time.sleep(0.05)
249
250
251 @example
252 def example18() -> None:
253 widgets = [Percentage(), ' ', Bar(), ' ', ETA(), ' ', AdaptiveETA()]
254 pbar = ProgressBar(widgets=widgets, maxval=500)
255 pbar.start()
256 for i in range(500):
257 time.sleep(0.01 + (i < 100) * 0.01 + (i > 400) * 0.9)
258 pbar.update(i + 1)
259 pbar.finish()
260
261
262 @example
263 def example19() -> None:
264 pbar = ProgressBar()
265 for _i in pbar([]):
266 pass
267 pbar.finish()
268
269
270 @example
271 def example20() -> None:
272 """Widgets that behave differently when length is unknown"""
273 widgets = [
274 '[When length is unknown at first]',
275 ' Progress: ',
276 SimpleProgress(),
277 ', Percent: ',
278 Percentage(),
279 ' ',
280 ETA(),
281 ' ',
282 AdaptiveETA(),
283 ]
284 pbar = ProgressBar(widgets=widgets, maxval=UnknownLength)
285 pbar.start()
286 for i in range(20):
287 time.sleep(0.5)
288 if i == 10:
289 pbar.maxval = 20
290 pbar.update(i + 1)
291 pbar.finish()
292
293
294 if __name__ == '__main__':
295 try:
296 for example in examples:
297 example()
298 except KeyboardInterrupt:
299 sys.stdout.write('\nQuitting examples.\n')