Python Simple progress bar Example __python

Source: Internet
Author: User
Tags flush

Recently need to write a small script python, with some small knowledge, hurriedly take time to record. not deep but often used.

Two progress bar example, the copy can be run:

# coding=utf-8

Import sys
import time

# width:   percent: Percent
def progress (width, percent):
    Print "\r%s%d%%"% ('%%-%ds '% width)% (width * percent/100 * ' = '), percent),
    if percent >=: print
  
   sys.stdout.flush ()


# example One, 0%--100%
def demo1 ():
    for I in Xrange (m):
        Progress (i + 1))
        Time.sleep (0.1)


# #  Example two, periodic load
def demo2 ():
    i = n = m = n
    > 0:
        print "\t\t\ t%s \ r "% (i *" = "),
        i = (i + 1)%
        Time.sleep (0.1)
        N-= 1


demo1 ()
Demo2 ()
  

Provides a simple asynchronous progress bar written by yourself that can be opened before a time-consuming operation and then stopped after a time-consuming operation.

Import time
import thread
import sys

class Progress:
    def __init__ (self):
        Self._flag = False
    def timer (self):
        i =
        self._flag:
            print "\t\t\t%s \ r"% (i * "="),
            Sys.stdout.flush ()
            i = (i + 1%
            time.sleep (0.05)
        print "\t\t\t%s\n"% (* "="),
        Thread.exit_thread ()
    def start (self): C15/>self._flag = True
        thread.start_new_thread (Self.timer, ())
    def Stop (self):
        Self._flag = False
        Time.sleep (1)

Usage:

progress = Progress ()
Progress.start ()
time.sleep (5)
progress.stop ()

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.