In connection with some calculations on python (for example, calculation of the coefficient of symmetry of the
CMB map), I wanted to make the process more visual. Well, at least add a progress bar. Bored, you know, sit in front of an empty console and stick to the running command. Whether it works, or hangs ... it is impossible to understand straight off. And I don’t want to do any performance logs, since then the eye is lost in the resulting chaos.
Recently found a way out: progressbar module. The module exists in deb-repositories (I found it in the ubuntu 9.10 repositories), put in the python-progressbar package:
Copy Source | Copy HTML sudo apt-get install python-progressbar
Copy Source | Copy HTML sudo apt-get install python-progressbar
For starters, you can try to run a small example:
Copy Source | Copy HTML
- #! / usr / bin / python
- # - * - coding: utf-8 - * -
- import progressbar
- import time
- bar = progressbar.ProgressBar (). start () # Create a new progress bar
- for t in xrange ( 101 ):
- bar.update (t) # This is the way to set the value.
- time .sleep ( 0.01 )
- bar.finish () # Finishing update - then you can
- print "finished =)"
Now a little more detail about the most simple and common use cases (as far as I can imagine them).
')
Firstly, in the constructor progressbar.ProgressBar (maxval = 100) there is an argument maxval, which specifies the maximum value in the bar. It can be both whole and real. Accordingly, in progressbar.ProgressBar.update () you can also transfer a real number.
Copy Source | Copy HTML
- #! / usr / bin / python
- # - * - coding: utf-8 - * -
- import progressbar
- import time
- bar = progressbar.ProgressBar (maxval = 10.0 ) .start ()
- t = 0.0
- while t <= 1.0 :
- bar.update (t)
- time .sleep ( 0.01 )
- t + = 0.01
- bar.finish ()
And finally, about tasty: widgets. Another argument to the progressbar.ProgressBar () constructor can be an array of widgets. Each widget has a number of its settings. The greatest amount, apparently, in pieces progressbar.Bar. From the following example, it should be obvious how easy and easy it is to use your widget set:
Copy Source | Copy HTML
- #! / usr / bin / python
- # - * - coding: utf-8 - * -
- import progressbar
- import time
- bar = progressbar.ProgressBar (maxval = 10.0 , widgets = [
- 'Just a progress bar test:' , # Static text
- progressbar.Bar (left = '[' , marker = '=' , right = ']' ), # Progress
- progressbar.ReverseBar (left = '[' , marker = '*' , right = ']' ), # Regress
- progressbar.SimpleProgress (), #The inscription "6 out of 10"
- ]). start ()
- t = 0.0
- while t <= 10.0 :
- bar.update (t)
- time .sleep ( 0.01 )
- t + = 0.1
- bar.finish ()

The module comes with detailed documentation. So there should be no problems with development. Although the last example, I think, covers 90% of all tasks.
Of the minuses of the module, one can note the reluctance to work in the Windows console.
UPD by
xvoland :
python-progressbar on pypi.python.org