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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

使用Python完成15位18位身份证的互转功能

使用Python完成15位18位身份证的互转功能

  最近工作中刚好要清洗一批客户数据,涉及到身份证号码15位和18位的转换,特意研究了下,在这里分享下。 身份证号码的构成 既然谈到了身份证转换,那就需要先了解下证件号码的构成...

python re正则表达式模块(Regular Expression)

模块的的作用主要是用于字符串和文本处理,查找,搜索,替换等 复习一下基本的正则表达式吧  .:匹配除了换行符以为的任意单个字符  *:匹配任意字符,一个,零个,多个都...

PyQt5根据控件Id获取控件对象的方法

如下所示: self.findChild(QComboBox, "name") self is class first parameter is Type second pa...

python anaconda 安装 环境变量 升级 以及特殊库安装的方法

Anaconda 是一个旗舰版的python安装包, 因为普通的python没有库, 如果需要安装一些重要的库, 要经常一个一个下载,会非常麻烦. 所以这个一个集成的, 可以手动批量升级...

Selenium元素的常用操作方法分析

本文实例讲述了Selenium元素的常用操作方法。分享给大家供大家参考,具体如下: Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的...