====== Progress Bar for Console Programs as Iterator ======
From [[http://code.activestate.com/recipes/576986-progress-bar-for-console-programs-as-iterator/|here]].
import sys
def progressbar(it, prefix = "", size = 60, show_every=1):
count = len(it)
def _show(_i):
if _i % show_every != 0: return
x = int(size*_i/count)
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):
yield item
_show(i+1)
sys.stdout.write("\n")
sys.stdout.flush()
Example use:
import time
for i in progressbar(range(15), "Computing: ", 40):
time.sleep(0.1) # long computation