python源碼:命令列下的進度條顯示

來源:互聯網
上載者:User

I was finding that I needed a progress indicator for Linux and Windows console applications that could be used to show the user that work was progressing and how much of the total work that had been completed. I finally broke down and wrote this class that seems to do exactly what I wanted. Since I continue to see questions about how to write such a class on Comp.Lang.Python, I thought I'd donate it to this Cookbook archive. 

# CLASS NAME: DLLInterface
#
# Author: Larry Bates (lbates@syscononline.com)
#
# Written: 12/09/2002
#
# Released under: GNU GENERAL PUBLIC LICENSE
#
#
class progressbarClass: 
    def __init__(self, finalcount, progresschar=None):
        import sys
        self.finalcount=finalcount
        self.blockcount=0
        #
        # See if caller passed me a character to use on the
        # progress bar (like "*").  If not use the block
        # character that makes it look like a real progress
        # bar.
        #
        if not progresschar: self.block=chr(178)
        else:                self.block=progresschar
        #
        # Get pointer to sys.stdout so I can use the write/flush
        # methods to display the progress bar.
        #
        self.f=sys.stdout
        #
        # If the final count is zero, don't start the progress gauge
        #
        if not self.finalcount : return
        self.f.write(' ------------------ % Progress -------------------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 ')
        return

    def progress(self, count):
        #
        # Make sure I don't try to go off the end (e.g. >100%)
        #
        count=min(count, self.finalcount)
        #
        # If finalcount is zero, I'm done
        #
        if self.finalcount:
            percentcomplete=int(round(100*count/self.finalcount))
            if percentcomplete < 1: percentcomplete=1
        else:
            percentcomplete=100
            
        #print "percentcomplete=",percentcomplete
        blockcount=int(percentcomplete/2)
        #print "blockcount=",blockcount
        if blockcount > self.blockcount:
            for i in range(self.blockcount,blockcount):
                self.f.write(self.block)
                self.f.flush()
                
        if percentcomplete == 100: self.f.write(" ")
        self.blockcount=blockcount
        return
    
if __name__ == "__main__":
    from time import sleep
    pb=progressbarClass(8,"*")
    count=0
    while count<9:
        count+=1
        pb.progress(count)
        sleep(0.2)

    pb=progressbarClass(100)
    pb.progress(20)
    sleep(0.2)
    pb.progress(47)
    sleep(0.2)
    pb.progress(90)
    sleep(0.2)
    pb.progress(100)
    print "testing 1:"
    pb=progressbarClass(1)
    pb.progress(1)

輸出結果:

 ------------------ % Progress -------------------1
    1    2    3    4    5    6    7    8    9    0
----0----0----0----0----0----0----0----0----0----0
**************************************************

------------------ % Progress -------------------1
    1    2    3    4    5    6    7    8    9    0
----0----0----0----0----0----0----0----0----0----0
ᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇ
testing 1:

------------------ % Progress -------------------1
    1    2    3    4    5    6    7    8    9    0
----0----0----0----0----0----0----0----0----0----0
ᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇᄇ

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.