views:

48

answers:

1

Hello,

I am trying to use progress bar to show the progress of a script. I want it increase progress after every function in a class is executed. The code I have tried is below:

import progressbar
from time import sleep

class hello():
    def no(self):
        print 'hello!'

    def yes(self):
        print 'No!!!!!!'


def pro():
    bar = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])

    for i in Yep():
        bar.update(Yep.i())
        sleep(0.1)
    bar.finish()

if __name__ == "__main__":
    Yep = hello()
    pro()

Does anyone know how to get this working. Thanks

+2  A: 

Hello, does this do what you want ?

import progressbar
from time import sleep

class hello():
    def no(self):
        print 'hello!'

    def yes(self):
        print 'No!!!!!!'

    def __call__(self) :
        methods = [self.no, self.yes]
        return [ (x[0]*100/len(methods), x[1]) for x in  enumerate(methods) ]

def pro():
    bar = progressbar.ProgressBar(widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])

    for percent, method in Yep():
        bar.update(percent)
        method()
        sleep(0.1)
    bar.finish()

if __name__ == "__main__":
    Yep = hello()
    pro()

Possible improvement : discover the methods to call by their name (for example prefix them with progress_)

yanjost
@yanjost -- That work's perfectly. Thanks for your help.
chrissygormley
You're welcome !
yanjost