Python source code: displays the progress bar under the command line

Source: Internet
Author: User

I was finding that I needed a progress indicator for Linux and Windows console applications that cocould 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
# 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 get 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)

Output result:

------------------ % 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
Please refer to the following link for more information: when there are too many threads, there are too many threads.
Testing 1:

------------------ % Progress ------------------- 1
1 2 3 4 5 6 7 8 9 0
---- 0----0----0----0----0----0----0----0----0------0
Please refer to the following link for more information: when there are too many threads, there are too many threads.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.