[蟒蛇菜谱] 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 ...
随机推荐
- 用NULL布局为什么不能显示
import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing. ...
- 复选框(checkbox)、单选框(radiobox)的使用
复选框(checkbox).单选框(radiobox)的使用 复选框: HTML: // 复选框 <input type="checkbox" name="chec ...
- Lost Cows(线段树 POJ2182)
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10354 Accepted: 6631 Descriptio ...
- What are the advantages of ReLU over sigmoid function in deep neural network?
The state of the art of non-linearity is to use ReLU instead of sigmoid function in deep neural netw ...
- java使用split切割字符串的时候,注意转义字符
今天在做项目的时候发现一个奇怪的问题 File file = new File("d:\\a.txt"); BufferedReader br = new BufferedRead ...
- InnoDB和MyISAM(转)
两种类型最主要的差别就是Innodb 支持事务处理与外键和行级锁.而MyISAM不支持. 我作为使用MySQL的用户角度出发,Innodb和MyISAM都是比较喜欢的,但是从我目前运维的数据库平台要达 ...
- AlwaysOn数据同步问题探究
随着AlwaysOn技术的流行,关于AlwayOn的问题也越来越多,某企业搭建有三副本的AlwaysOn一套,现想修改主节点上某张表的某个数据,看看会出现什么后果,如果结果正常,就同步到其他节点上:如 ...
- English Literature
The website links of English Literature,which I wanno recommend to U is based on following. 数据结构 - 知 ...
- lodash接触:string-capitalize
1.capitalize : _.capitalize([string='']) Capitalizes the first character of string. 参数 [string=''] ( ...
- JavaScipt 源码解析 回调函数
函数是第一类对象,这是javascript中的一个重要的概念,意味着函数可以像对象一样按照第一类管理被使用,所以在javascript中的函数: 能"存储"在变量中,能作为函数的实 ...