[蟒蛇菜谱] Python方便使用的级联进度信息

本文介绍了一种用于显示进度的级联进度信息类,包括初始化、子进度管理、通知父进度等功能,并通过示例展示了如何使用该类进行级联进度的显示与更新。
class StepedProgress:
    '''方便显示进度的级联进度信息。
    '''
    def __init__(self, stockPercent=[1], parentProgress=None):
        self.percent = 0
        self.info = ''
        self.subProgress = []
        self.cur_running_process = 0
        self.stockPercent = stockPercent
        self.parentProgress = parentProgress

        # 重新计算进度比,防止初始化时的值加起来不是1
        w = 0.0
        for p in self.stockPercent:
            w += p
        for i in range(0, len(stockPercent)):
            stockPercent[i] = stockPercent[i]/w

        # 初始化子进度
        if len(stockPercent) == 1:
            self.subProgress = None
        else:
            for p in self.stockPercent:
                self.subProgress.append(StepedProgress(parentProgress=self))

    def subprogress(self, index):
        if index >= self.subcount():
            return self.subProgress[self.subcount()-1]
        elif index < self.cur_running_process:
            return self.subProgress[self.cur_running_process]
        else:
            self.cur_running_process = index
            return self.subProgress[index]

    def subcount(self):
        return len(self.subProgress)

    def notifyParentProgress(self, percent, info=None):
        new_percent = 0.0
        for i in range(0, self.cur_running_process):
            new_percent += self.stockPercent[i]
        new_percent += percent/100.0 * self.stockPercent[self.cur_running_process]
        new_percent *= 100.0
        self.notifyProgress(new_percent, info)

    def notifyProgress(self, percent, info=None):
        if percent > self.percent:
            self.percent = percent
        if info is not None:
            self.info = info
        if self.parentProgress is not None:
            self.parentProgress.notifyParentProgress(percent, info)
        else:
            print self.info[:77].ljust(80, '.'), "[%0.1f%%]"%self.percent

if __name__ == "__main__":
    s = StepedProgress([60, 40])
    s.notifyProgress(10, 'aaa')

    s1 = s.subprogress(0)
    s1.notifyProgress(50, 'bbb')

    s3 = s.subprogress(1)
    s3 = StepedProgress([1, 1], parentProgress=s3.parentProgress) #级联子进度
    s3.notifyProgress(20, 'ddd')

    s4 = s3.subprogress(0)
    s4.notifyProgress(50, 'eee')

    s5 = s3.subprogress(1)
    s5.notifyProgress(50, 'fff')

 

输出结果:

aaa............................................................................. [10.0%]
bbb............................................................................. [30.0%]
ddd............................................................................. [68.0%]
eee............................................................................. [70.0%]
fff............................................................................. [90.0%]

转载于:https://www.cnblogs.com/tuzkee/p/3668093.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值