学不下去了,来学一学进度条减压 通过借鉴别人的代码和Python已有的进度条包进而实现自己的进度条
进度条有两种模式:simple:不显示时间和总进度,standard:显示时间和总进度数 代码如下
import sys
import time
class ProgressBar():
def __init__(self, max,mode='standard', hint='Progress', ):
self.mode=mode
self.hint=hint
self.max=max
self.progress=0
def set_mode(self,mode):self.mode=mode
def set_hint(self,hint):self.hint=hint
def start(self):
if self.mode!='simple':self.start_ = time.perf_counter()
def next(self,info=''):
self.progress+=1
progress=self.progress
all_work=self.max
i = int(progress / all_work * 100)
if self.mode=='simple':
print("\r", end="")
print(self.hint+": {}%: ".format(i), "▋" * (i // 2), end="")
sys.stdout.flush()
elif self.mode=='standard':
scale=100
a = "*" * i
b = "." * (scale - i)
c = (i / scale) * 100
try:
dur = time.perf_counter() - self.start_
print( "\r"+self.hint+ " "+info+" {:^3.0f}%[{}->{}]{:.2f}s".format(c, a, b, dur),end='')
except:
print("ProgressBar:你没有start")
测试该进度条
def test():
num = 1000
bar=ProgressBar(mode='standard',max=num)
bar.start()
for i in range(1, num):
time.sleep(0.01)
bar.next()
|