发布于 2015-10-06 02:23:57 | 143 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python入门,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


这篇文章主要介绍了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%]



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务