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)
            
  • 相关阅读:
    51nod 1051【基础】
    HDU5971【瞎搞】
    Lightoj1018 【状压DP】
    HDU2604【矩阵快速幂】
    HDU1501【简单DP】
    HDU3555【数位DP】
    Lightoj1037【状压DP】
    51nod 1099【贪心】
    HDU5950【矩阵快速幂】
    51nod 1049【经典】
  • 原文地址:https://www.cnblogs.com/LittleRedPoint/p/4007463.html
Copyright © 2011-2022 走看看