[蟒蛇菜谱] 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封装shell命令
# -*- coding: utf-8 -*- import os import subprocess import signal import pwd import sys class MockLo ...
- [蟒蛇菜谱]Python日志记录最佳实践
# -*- coding: utf8 -*- import logging # 创建一个logger logger = logging.getLogger('mylogger') logger.set ...
- [蟒蛇菜谱]Python函数参数传递最佳实践
将函数作为参数传递,同时将该函数需要的参数一起传递.可参考threading.Timer的处理方式: class threading.Timer(interval, function, args=[] ...
- [蟒蛇菜谱]Python获取任意xml节点的值
# -*- coding: utf-8 -*- import xml.dom.minidom ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE class Simple ...
- [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】
[python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...
- Python 抓取网页并提取信息(程序详解)
最近因项目需要用到python处理网页,因此学习相关知识.下面程序使用python抓取网页并提取信息,具体内容如下: #---------------------------------------- ...
- 如何使用 WinInet 时提供下载上载进度信息
概要许多开发人员都使用 WinInet 函数来下载或上载文件在 Internet 上的想要提供一个进度条以指示多少文件传输已完成,但多少就越长.您可以使用以下机制来完成此.Collapse image ...
- 【原创】用JAVA实现大文件上传及显示进度信息
用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...
- python使用post登陆电子科大信息门户并保存登陆后页面
python使用post登陆电子科大信息门户并保存登陆后页面 作者:vpoet mail:vpoet_sir@163.com #coding=utf-8 import HTMLParser impor ...
随机推荐
- dotfiles管理
刚刚知道dotfiles这个东西,百度也没发现什么太有价值的讲解,还都是英文,所以自己立志来好好屡屡清楚 1.dotfiles是什么?我自己的理解:linux下(mac下)有各种app,每个人会根据自 ...
- Java 基础知识 练习
1.在DOS命令下输入:java Hello出现以下结果:Bad command or the file name可能是什么原因? (错误的命令或文件名) 输入的命令不存在,或者不在指定的路径中 2. ...
- Unix网络编程--卷二:FAQ
1.编译unpipc库. 执行./configure时报错: checking host system type... Invalid configuration `x86_64-pc-linux-g ...
- CentOS 安装Zookeeper-3.4.6 单节点
Dubbo 建议使用 Zookeeper 作为服务的注册中心. 注册中心服务器(192.168.3.71)配置,安装 Zookeeper: 1. 修改操作系统的/etc/hosts 文件中添加: # ...
- [问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供)
[问题2014A01] 解答一(第一列拆分法,由张钧瑞同学提供) (1) 当 \(a=0\) 时,这是高代书复习题一第 33 题,可用升阶法和 Vander Monde 行列式来求解,其结果为 \[ ...
- 从零开始学iPhone开发(5)——使用MapKit
(转)Leonbao:MapKit学习笔记 1.概述插入MapView,设置Delegate(一般为Controller),Annotations记录兴趣位置点(AnnotationView用来显示兴 ...
- DAO
DAO Data Access Object DAO(Data Access Object)是一个数据访问接口,数据访问:顾名思义就是与数据库打交道. 夹在业务逻辑与数据库资源中间. DAO模式是标准 ...
- Android:Butter Knife 8.0.1配置
github地址:https://github.com/GarsonZhang/butterknife Butter Knife Field and method binding for Androi ...
- python文件和目录操作方法大全(含实例)
一.python中对文件.文件夹操作时经常用到的os模块和shutil模块常用方法.1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()2.返回指定目录下的所有文件和目 ...
- Selenium Web 自动化 - 项目实战(三)
Selenium Web 自动化 - 项目实战(三) 2016-08-10 目录 1 关键字驱动概述2 框架更改总览3 框架更改详解 3.1 解析新增页面目录 3.2 解析新增测试用例目录 3. ...