python实现方便使用的级联进度信息实例

本文实例讲述了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%]

希望本文所述对大家的python程序设计有所帮助。

Posted in 未分类