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

相关文章

详解django自定义中间件处理

中间件是一个钩子框架,它们可以介入 Django 的请求和响应处理过程。 它是一个轻量级、底层的 插件 系统,用于在 全局修改 Django 的输入或输出 。 每个中间件组件负责完成某个...

PHP webshell检查工具 python实现代码

1.使用方法:find.py 目录名称 2. 主要是采用python正则表达式来匹配的,可以在keywords中添加自己定义的正则,格式: ["eval\(\$\_POST","发现PH...

python中文编码问题小结

python中文编码问题小结

中文编码问题一直是Python程序设计中很头痛的问题,本文对此较为详细的进行了总结归纳。具体如下:   当字符串是:'\u4e2d\u56fd'  >&g...

PyCharm搭建Spark开发环境实现第一个pyspark程序

PyCharm搭建Spark开发环境实现第一个pyspark程序

一, PyCharm搭建Spark开发环境 Windows7, Java1.8.0_74, Scala 2.12.6, Spark 2.2.1, Hadoop2.7.6 通常情况下,Sp...

pymssql数据库操作MSSQL2005实例分析

本文实例讲述了pymssql数据库操作MSSQL2005的方法。分享给大家供大家参考。具体如下: 使用的MSSQL2005,通过pymssql来连接的。把可能用到的数据库操作方式都总结如...