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

yipeiwu_com6年前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中str字符串和unicode对象字符串的拼接问题

str字符串 s = '中文' # s: <type 'str'> s是个str对象,中文字符串。存储方式是字节码。字节码是怎么存的: 如果这行代码在python解释...

python笔记之mean()函数实现求取均值的功能代码

用法:mean(matrix,axis=0)  其中 matrix为一个矩阵,axis为参数 以m * n矩阵举例: axis 不设置值,对 m*n 个数求均值,返回一个实数...

解决python给列表里添加字典时被最后一个覆盖的问题

如下所示: >>> item={} ; items=[] #先声明一个字典和一个列表,字典用来添加到列表里面 >>> item['index']...

python字符串切割:str.split()与re.split()的对比分析

1、str.split不支持正则及多个切割符号,不感知空格的数量,比如用空格切割,会出现下面情况。 >>> s1="aa bb cc" >>> s...

Python3.5装饰器原理及应用实例详解

Python3.5装饰器原理及应用实例详解

本文实例讲述了Python3.5装饰器原理及应用。分享给大家供大家参考,具体如下: 1、装饰器: (1)本质:装饰器的本质是函数,其基本语法都是用关键字def去定义的。 (2)功能:装饰...