Skip to content

Instantly share code, notes, and snippets.

@hit9
Created May 21, 2013 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hit9/5619913 to your computer and use it in GitHub Desktop.
Save hit9/5619913 to your computer and use it in GitHub Desktop.
打印一个百分进度
import sys
import time
from termcolor import colored
class Progress(object):
def __init__(self):
size = 0
description = ''
def new(self, size=0, description=''):
self.size = size
self.description = description
self.counter = 0
self.text = None
def check(self, text):
self.counter += 1
if self.text:
sys.stdout.write("\r" + " " * (len(self.description) + 8 + len(self.text)))
sys.stdout.write("\r")
sys.stdout.write(self.description)
sys.stdout.write(" [%3d%%]" % int(100 * self.counter / self.size))
sys.stdout.write(" %s" % text)
sys.stdout.flush()
self.text = text
progress = Progress()
progress.new(size=120, description="Do sothing")
for i in range(10):
progress.check(str(13-i))
time.sleep(0.25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment