Source

woocode / py / progress_bar_1.py

import time
import sys

toolbar_width = 72

class ProgressBar(object):
    def __init__(self, bar_length, char='#'):
        self.bar_length = bar_length
        self.char = '#'
        self.fd = sys.stderr
        # self.fd = sys.stdout
        self.curval = 0
        self.minval = 1
        self.maxval = bar_length + 2

    def start(self):
        # self.fd.write("[%s]" % (" " * self.bar_length))
        # self.fd.flush()
        # self.fd.write("\b" * (self.bar_length + 1))
        return self

    def update(self, v):
        self.fd.write('|' + (v * self.char).ljust(self.bar_length) + '|' + '\r')
        # self.fd.flush()


def percentage(curval, value):
    return '%3d%%' % curval * 100.0 / value


#def init_progress_bar(bar_length):
    ## setup  toolbar
    #sys.stdout.write("[%s]" % (" " * bar_length))
    #sys.stdout.flush()
    ## \b => backspace
    #sys.stdout.write("\b" * (bar_length + 1))

#def update_bar(char='#'):
    #sys.stdout.write(char)
    #sys.stdout.flush()

pbar = ProgressBar(toolbar_width).start()
for i in range(toolbar_width):
    time.sleep(0.1)
    # perc = percentage(i, toolbar_width)
    pbar.update(i)

# sys.stdout.write("\n")
print