码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-10-06 01:03:19      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   c   log   

任务:

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

解决方案:

#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 在文本控制台中显示进度条

标签:style   blog   color   ar   for   sp   div   c   log   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!