Python progress bar shows sample code for processing progress in real time, and python sample code

Source: Internet
Author: User

Python progress bar shows sample code for processing progress in real time, and python sample code

Preface

In most cases, our program will continue to process cyclically. At this time, we hope to know the processing progress of the program and decide what to do next. Next we will show you how to implement this function easily and beautifully.

How to use this class

This class is easy to use and can be completed in three steps:

Process_bar = ShowProcess (max_steps) #1. define the object of the class before the loop. max_steps is the total number of steps for I in range (max_steps + 1): process_bar.show_process () #2. displays the current progress time. sleep (0.05) process_bar.close ('done') #3. message displayed after processing

Progress bar implementation

Because it is very simple, directly add the code

#! /Usr/local/lib #-*-coding: UTF-8-*-import sys, timeclass ShowProcess (): "indicates the processing progress. You can call this function to display the processing progress." I = 0 # current processing progress max_steps = 0 # Total number of times max_arrow = 50 # length of the progress bar # initialization function, the total number of processing times def _ init _ (self, max_steps): self. max_steps = max_steps self. I = 0 # display function, displays the progress based on the current processing progress. # The result is [>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>] 100.00% def show_process (self, I = None): if I is not None: self. I = I else: self. I + = 1 num_arrow = int (self. I * self. max_arrow/self. max_steps) # calculate the number of displayed '> 'num_line = self. max_arrow-num_arrow # calculates the number of '-' percent = self. I X 100.0/self. max_steps # computing progress, in the format of xx. xx % process_bar = '[' + '>' * num_arrow + '-' * num_line + ']' \ + '%. 2f '% percent +' % '+' \ R' # string with output. '\ R' indicates that no line break is returned to the leftmost sys. stdout. write (process_bar) # print the two sentences to the terminal sys. stdout. flush () def close (self, words = 'done'): print ''print words self. I = 0if _ name __= = '_ main _': max_steps = 100 process_bar = ShowProcess (max_steps) for I in range (max_steps + 1): process_bar.show_process () time. sleep (0.05) process_bar.close ()

Run

You can call this class according to the method of the main function. The result is as follows:

[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>] 100.00%
Done

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.