1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3
4import sys
5import time
6
7from progressbar import AnimatedMarker, Bar, BouncingBar, Counter, ETA, \
8    AdaptiveETA, FileTransferSpeed, FormatLabel, Percentage, \
9    ProgressBar, ReverseBar, RotatingMarker, \
10    SimpleProgress, Timer
11
12examples = []
13def example(fn):
14    try: name = 'Example %d' % int(fn.__name__[7:])
15    except: name = fn.__name__
16
17    def wrapped():
18        try:
19            sys.stdout.write('Running: %s\n' % name)
20            fn()
21            sys.stdout.write('\n')
22        except KeyboardInterrupt:
23            sys.stdout.write('\nSkipping example.\n\n')
24
25    examples.append(wrapped)
26    return wrapped
27
28@example
29def example0():
30    pbar = ProgressBar(widgets=[Percentage(), Bar()], maxval=300).start()
31    for i in range(300):
32        time.sleep(0.01)
33        pbar.update(i+1)
34    pbar.finish()
35
36@example
37def example1():
38    widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()),
39               ' ', ETA(), ' ', FileTransferSpeed()]
40    pbar = ProgressBar(widgets=widgets, maxval=10000000).start()
41    for i in range(1000000):
42        # do something
43        pbar.update(10*i+1)
44    pbar.finish()
45
46@example
47def example2():
48    class CrazyFileTransferSpeed(FileTransferSpeed):
49        """It's bigger between 45 and 80 percent."""
50        def update(self, pbar):
51            if 45 < pbar.percentage() < 80:
52                return 'Bigger Now ' + FileTransferSpeed.update(self,pbar)
53            else:
54                return FileTransferSpeed.update(self,pbar)
55
56    widgets = [CrazyFileTransferSpeed(),' <<<', Bar(), '>>> ',
57               Percentage(),' ', ETA()]
58    pbar = ProgressBar(widgets=widgets, maxval=10000000)
59    # maybe do something
60    pbar.start()
61    for i in range(2000000):
62        # do something
63        pbar.update(5*i+1)
64    pbar.finish()
65
66@example
67def example3():
68    widgets = [Bar('>'), ' ', ETA(), ' ', ReverseBar('<')]
69    pbar = ProgressBar(widgets=widgets, maxval=10000000).start()
70    for i in range(1000000):
71        # do something
72        pbar.update(10*i+1)
73    pbar.finish()
74
75@example
76def example4():
77    widgets = ['Test: ', Percentage(), ' ',
78               Bar(marker='0',left='[',right=']'),
79               ' ', ETA(), ' ', FileTransferSpeed()]
80    pbar = ProgressBar(widgets=widgets, maxval=500)
81    pbar.start()
82    for i in range(100,500+1,50):
83        time.sleep(0.2)
84        pbar.update(i)
85    pbar.finish()
86
87@example
88def example5():
89    pbar = ProgressBar(widgets=[SimpleProgress()], maxval=17).start()
90    for i in range(17):
91        time.sleep(0.2)
92        pbar.update(i + 1)
93    pbar.finish()
94
95@example
96def example6():
97    pbar = ProgressBar().start()
98    for i in range(100):
99        time.sleep(0.01)
100        pbar.update(i + 1)
101    pbar.finish()
102
103@example
104def example7():
105    pbar = ProgressBar()  # Progressbar can guess maxval automatically.
106    for i in pbar(range(80)):
107        time.sleep(0.01)
108
109@example
110def example8():
111    pbar = ProgressBar(maxval=80)  # Progressbar can't guess maxval.
112    for i in pbar((i for i in range(80))):
113        time.sleep(0.01)
114
115@example
116def example9():
117    pbar = ProgressBar(widgets=['Working: ', AnimatedMarker()])
118    for i in pbar((i for i in range(50))):
119        time.sleep(.08)
120
121@example
122def example10():
123    widgets = ['Processed: ', Counter(), ' lines (', Timer(), ')']
124    pbar = ProgressBar(widgets=widgets)
125    for i in pbar((i for i in range(150))):
126        time.sleep(0.1)
127
128@example
129def example11():
130    widgets = [FormatLabel('Processed: %(value)d lines (in: %(elapsed)s)')]
131    pbar = ProgressBar(widgets=widgets)
132    for i in pbar((i for i in range(150))):
133        time.sleep(0.1)
134
135@example
136def example12():
137    widgets = ['Balloon: ', AnimatedMarker(markers='.oO@* ')]
138    pbar = ProgressBar(widgets=widgets)
139    for i in pbar((i for i in range(24))):
140        time.sleep(0.3)
141
142@example
143def example13():
144    # You may need python 3.x to see this correctly
145    try:
146        widgets = ['Arrows: ', AnimatedMarker(markers='←↖↑↗→↘↓↙')]
147        pbar = ProgressBar(widgets=widgets)
148        for i in pbar((i for i in range(24))):
149            time.sleep(0.3)
150    except UnicodeError: sys.stdout.write('Unicode error: skipping example')
151
152@example
153def example14():
154    # You may need python 3.x to see this correctly
155    try:
156        widgets = ['Arrows: ', AnimatedMarker(markers='◢◣◤◥')]
157        pbar = ProgressBar(widgets=widgets)
158        for i in pbar((i for i in range(24))):
159            time.sleep(0.3)
160    except UnicodeError: sys.stdout.write('Unicode error: skipping example')
161
162@example
163def example15():
164    # You may need python 3.x to see this correctly
165    try:
166        widgets = ['Wheels: ', AnimatedMarker(markers='◐◓◑◒')]
167        pbar = ProgressBar(widgets=widgets)
168        for i in pbar((i for i in range(24))):
169            time.sleep(0.3)
170    except UnicodeError: sys.stdout.write('Unicode error: skipping example')
171
172@example
173def example16():
174    widgets = [FormatLabel('Bouncer: value %(value)d - '), BouncingBar()]
175    pbar = ProgressBar(widgets=widgets)
176    for i in pbar((i for i in range(180))):
177        time.sleep(0.05)
178
179@example
180def example17():
181    widgets = [FormatLabel('Animated Bouncer: value %(value)d - '),
182               BouncingBar(marker=RotatingMarker())]
183
184    pbar = ProgressBar(widgets=widgets)
185    for i in pbar((i for i in range(180))):
186        time.sleep(0.05)
187
188@example
189def example18():
190    widgets = [Percentage(),
191               ' ', Bar(),
192               ' ', ETA(),
193               ' ', AdaptiveETA()]
194    pbar = ProgressBar(widgets=widgets, maxval=500)
195    pbar.start()
196    for i in range(500):
197        time.sleep(0.01 + (i < 100) * 0.01 + (i > 400) * 0.9)
198        pbar.update(i + 1)
199    pbar.finish()
200
201@example
202def example19():
203  pbar = ProgressBar()
204  for i in pbar([]):
205    pass
206  pbar.finish()
207
208if __name__ == '__main__':
209    try:
210        for example in examples: example()
211    except KeyboardInterrupt:
212        sys.stdout.write('\nQuitting examples.\n')
213