User Tools

Site Tools


python:progress_bar

Progress Bar for Console Programs as Iterator

From 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
python/progress_bar.txt · Last modified: 2012/08/30 17:33 by tkbletsc