首页 > 其他 > 详细

cookbook 11.1 在文本控制台中显示进度条

时间:2014-10-05 23:08:29      阅读:369      评论:0      收藏:0      [点我收藏+]

任务:

在进行长时间操作时,向用户显示一个"进度指示条"。

解决方案:

#coding=utf-8
import sys
class progressbar(object):
    def __init__(self,finalcount,block_char=.):
        self.finalcount = finalcount
        self.blockcount = 0
        self.block = block_char
        self.f = sys.stdout
        if not self.finalcount: return
        self.f.write(\n-------- % Process ------- 1\n)
        self.f.write(  1  2  3  4  5  6  7  8  9  0\n)
        self.f.write(  0  0  0  0  0  0  0  0  0  0\n)

    def progress(self,count):
        count = min(count,self.finalcount)
        if self.finalcount:
            percentcomplent = int(round(100.0*count/self.finalcount))
            if percentcomplent < 1: percentcomplent = 1
        else:
            percentcomplent = 100
        blockcount = int(percentcomplent//2)#//就是除法,但不四舍五入
        if blockcount <= self.blockcount:
            return
        for i in range(self.blockcount,blockcount):
            self.f.write(self.block)
        self.f.flush()
        self.blockcount = blockcount
        if percentcomplent == 100:
            self.f.write("\n")
        

#test
if __name__ == __main__:
    from time import sleep
    pb = progressbar(8,"*")
    for count in range(1,9):
        pb.progress(count)
        sleep(0.2)
    pb = progressbar(100)
    pb.progress(20)
    sleep(0.3)
    pb.progress(47)
    sleep(0.3)
    pb.progress(90)
    sleep(0.3)
    pb.progress(100)
    print "testing 1:"
    pb = progressbar(1)
    pb.progress(1)
        

 

cookbook 11.1 在文本控制台中显示进度条

原文:http://www.cnblogs.com/LittleRedPoint/p/4007463.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!