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两台电脑实现TCP通信的方法示例

为了实现Nao机器人与电脑端的TCP通信,于是研究了一下Python实现TCP通信,在网上也看到了很多例子,但大多都是在一台机器上验证。在两台机器上使用,出了一些小故障。 注意:若两台电...

python黑魔法之参数传递

我们都听说,python世界里面,万物皆对象。 怎么说万物皆对象呢?最常见的: > class A: pass > a = A() 我们说a是一个对象。 那么既然是万...

python在windows命令行下输出彩色文字的方法

本文实例讲述了python在windows命令行下输出彩色文字的方法。分享给大家供大家参考。具体分析如下: 默认情况下python在控制台输出的文字信息都是黑白的,如果能将文字做成彩色的...

Python 3中print函数的使用方法总结

前言 Python 思想:“一切都是对象!”,最近发现python3和python2中print的用法有很多不同,python3中需要使用括号,缩进要使用4个空格(这不是必须的,但你最好...

Flask入门教程实例:搭建一个静态博客

Flask入门教程实例:搭建一个静态博客

现在流行的静态博客/网站生成工具有很多,比如 Jekyll, Pelican, Middleman, Hyde 等等,StaticGen 列出了目前最流行的一些静态网站生成工具。 我们的...