Created
May 21, 2013 13:50
-
-
Save hit9/5619913 to your computer and use it in GitHub Desktop.
打印一个百分进度
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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