Python實現方便使用的級聯進度資訊執行個體

來源:互聯網
上載者:User
本文執行個體講述了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.percentif __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程式設計有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.