首页 > 代码库 > cookbook 11.1 在文本控制台中显示进度条
cookbook 11.1 在文本控制台中显示进度条
任务:
在进行长时间操作时,向用户显示一个"进度指示条"。
解决方案:
#coding=utf-8import sysclass progressbar(object): def __init__(self,finalcount,block_char=‘.‘): self.finalcount = finalcount self.blockcount = 0 self.block = block_char self.f = sys.stdout if not self.finalcount: return self.f.write(‘\n-------- % Process ------- 1\n‘) self.f.write(‘ 1 2 3 4 5 6 7 8 9 0\n‘) self.f.write(‘ 0 0 0 0 0 0 0 0 0 0\n‘) def progress(self,count): count = min(count,self.finalcount) if self.finalcount: percentcomplent = int(round(100.0*count/self.finalcount)) if percentcomplent < 1: percentcomplent = 1 else: percentcomplent = 100 blockcount = int(percentcomplent//2)#//就是除法,但不四舍五入 if blockcount <= self.blockcount: return for i in range(self.blockcount,blockcount): self.f.write(self.block) self.f.flush() self.blockcount = blockcount if percentcomplent == 100: self.f.write("\n") #testif __name__ == ‘__main__‘: from time import sleep pb = progressbar(8,"*") for count in range(1,9): pb.progress(count) sleep(0.2) pb = progressbar(100) pb.progress(20) sleep(0.3) pb.progress(47) sleep(0.3) pb.progress(90) sleep(0.3) pb.progress(100) print "testing 1:" pb = progressbar(1) pb.progress(1)
cookbook 11.1 在文本控制台中显示进度条
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。