User Tools

Site Tools


python:progress_bar

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:progress_bar [2010/07/06 07:32]
tkbletsc created
python:progress_bar [2012/08/30 17:33] (current)
tkbletsc
Line 5: Line 5:
 import sys import sys
  
-def progressbar(it,​ prefix = "",​ size = 60): +def progressbar(it,​ prefix = "",​ size = 60, show_every=1): 
-    count = len(it) + count = len(it) 
-    def _show(_i):​ + def _show(_i):​ 
-        x = int(size*_i/​count) + if _i % show_every != 0: return 
-        sys.stdout.write("​%s[%s%s] %i/​%i\r"​ % (prefix, "#"​*x,​ "​."​*(size-x),​ _i, count)) + x = int(size*_i/​count) 
-        sys.stdout.flush() + sys.stdout.write("​%s[%s%s] %i/​%i\r"​ % (prefix, "#"​*x,​ "​."​*(size-x),​ _i, count)) 
-     + sys.stdout.flush() 
-    _show(0) +  
-    for i, item in enumerate(it):​ + _show(0) 
-        yield item + for i, item in enumerate(it):​ 
-        _show(i+1) + yield item 
-    sys.stdout.write("​\n"​) + _show(i+1) 
-    sys.stdout.flush()+ sys.stdout.write("​\n"​) 
 + sys.stdout.flush()
  
 </​code>​ </​code>​
Line 28: Line 29:
     time.sleep(0.1) # long computation     time.sleep(0.1) # long computation
 </​code>​ </​code>​
- 
python/progress_bar.txt · Last modified: 2012/08/30 17:33 by tkbletsc