首页 编程教程正文

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

piaodoo 编程教程 2020-02-02 12:21:05 1073 0 python教程

这篇文章主要介绍了Python实现方便使用的级联进度信息,实例分析了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程序设计有所帮助。

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/1013.html

搜索