zoukankan      html  css  js  c++  java
  • cookbook 11.1 在文本控制台中显示进度条

    任务:

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

    解决方案:

    #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('
    -------- % Process ------- 1
    ')
            self.f.write('  1  2  3  4  5  6  7  8  9  0
    ')
            self.f.write('  0  0  0  0  0  0  0  0  0  0
    ')
    
        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("
    ")
            
    
    #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)
            
  • 相关阅读:
    Mysql备份和恢复
    前端Css学习
    jQuery学习
    HTML页面学习
    Linux下java环境变量配置
    windows下java环境变量标准配置
    oracle查询消耗服务器资源SQL语句
    Java主线程在子线程执行完毕后再执行
    CentOS7 安装 Redis
    查看Oracle表空间使用情况
  • 原文地址:https://www.cnblogs.com/LittleRedPoint/p/4007463.html
Copyright © 2011-2022 走看看