如何获得进度条到时间类执行

时间:2010-06-09 09:24:22

标签: python class progress-bar

我正在尝试使用进度条来显示脚本的进度。我希望它在执行类中的每个函数后增加进度。我试过的代码如下:

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()

有谁知道如何让这个工作。感谢

1 个答案:

答案 0 :(得分:2)

这样做你想要的吗?

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()

可能的改进:发现按名称调用的方法(例如,使用进度_作为前缀)