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 ()