【问题标题】:How to get progress bar to time Class exectution如何让进度条计时类执行
【发布时间】:2010-06-09 09:24:22
【问题描述】:

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

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

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

【问题讨论】:

    标签: python class progress-bar


    【解决方案1】:

    这是你想要的吗?

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

    可能的改进:发现按名称调用的方法(例如在它们前面加上progress_)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 2016-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多