2010-01-17 03:48:23 +01:00
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
class Progress():
|
2010-01-17 16:43:42 +01:00
|
|
|
def __init__(self, maxvalue, maxwidth=79, fd=sys.stdout):
|
2010-01-17 03:48:23 +01:00
|
|
|
self.maxwidth = maxwidth
|
|
|
|
self.maxvalue = maxvalue
|
|
|
|
self.fd = fd
|
2010-01-17 16:43:42 +01:00
|
|
|
self.prog_char = '#'
|
|
|
|
self.fill_cahr = ' '
|
2010-01-17 03:48:23 +01:00
|
|
|
|
|
|
|
self.show_progress(0)
|
|
|
|
|
|
|
|
def show_progress(self, value):
|
|
|
|
str_value = str(value)
|
|
|
|
str_maxvalue = str(self.maxvalue)
|
|
|
|
true_maxwidth = self.maxwidth - 4 - len(str_value) - len(str_maxvalue)
|
|
|
|
progress = int(round((true_maxwidth/float(self.maxvalue))*value))
|
2010-01-17 16:43:42 +01:00
|
|
|
self.fd.write("\r%s/%s [%s%s]" % (str_value, str_maxvalue, self.prog_char * progress, self.fill_char * (true_maxwidth - progress)))
|
2010-01-17 03:48:23 +01:00
|
|
|
self.fd.flush()
|
|
|
|
if value == self.maxvalue:
|
|
|
|
self.fd.write("\n")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
prog = Progress(200)
|
|
|
|
for i in range(1, 201):
|
|
|
|
prog.show_progress(i)
|
2010-01-17 16:43:42 +01:00
|
|
|
time.sleep(0.1)
|