Python實現(xiàn)方便使用的級聯(lián)進度信息實例
本文實例講述了Python實現(xiàn)方便使用的級聯(lián)進度信息的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
class StepedProgress:
'''方便顯示進度的級聯(lián)進度信息。
'''
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) #級聯(lián)子進度
s3.notifyProgress(20, 'ddd')
s4 = s3.subprogress(0)
s4.notifyProgress(50, 'eee')
s5 = s3.subprogress(1)
s5.notifyProgress(50, 'fff')
輸出結(jié)果:
aaa............................................................................. [10.0%]
bbb............................................................................. [30.0%]
ddd............................................................................. [68.0%]
eee............................................................................. [70.0%]
fff............................................................................. [90.0%]
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Python面向?qū)ο缶幊讨嘘P(guān)于類和方法的學(xué)習(xí)筆記
類與類方法是面向?qū)ο蟮木幊陶Z言中必不可少的特性,本文總結(jié)了Python面向?qū)ο缶幊讨嘘P(guān)于類和方法的學(xué)習(xí)筆記,需要的朋友可以參考下2016-06-06
Python 快速驗證代理IP是否有效的方法實現(xiàn)
有時候,我們需要用到代理IP,比如在爬蟲的時候,不知道怎么驗證這些IP是不是有效的,本文就介紹一下,感興趣的可以了解一下2021-07-07
Python中enumerate()函數(shù)詳細分析(附多個Demo)
Python的enumerate()函數(shù)是一個內(nèi)置函數(shù),主要用于在遍歷循環(huán)中獲取每個元素的索引以及對應(yīng)的值,這篇文章主要介紹了Python中enumerate()函數(shù)的相關(guān)資料,需要的朋友可以參考下2024-10-10
Python如何查看兩個數(shù)據(jù)庫的同名表的字段名差異
這篇文章主要介紹了Python如何查看兩個數(shù)據(jù)庫的同名表的字段名差異,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
Python正則獲取、過濾或者替換HTML標(biāo)簽的方法
這篇文章主要介紹了Python通過正則表達式獲取、過濾或者替換HTML標(biāo)簽的方法,感興趣的小伙伴們可以參考一下2016-01-01

