imglong是进度条长度,maxid是最大循环次数,curid是当前循环次数,进度条为了方便处理设置成了20或者50的倍数(都是100的整倍),免得去处理浮点数-v-
class ProcessSchedule(object):
"""
This class use to print porecess schedule
ImgLong usr to define max long of img and it will be set as 20 or n*50
"""
def __init__(self,maxid =1,ImgLong=50,startPercent = 0):
"""self.percenct is the cur percence of porcess"""
self.percnet = startPercent
self.ImgLong = ImgLong
self.maxid = maxid
def PrintSchedule(self,curid):
if curid > self.maxid:return
if self.ImgLong < 50:
self.ImgLong = 20
if self.ImgLong > 50:
self.ImgLong = (int(self.ImgLong/50))*50
self.percent = int((curid*100)/self.maxid)
rangeStep = int(100/self.ImgLong)
if rangeStep == 0:rangeStep =1
for i in range(self.percent,101,rangeStep):
if self.percent == i:
count = int((i*self.ImgLong)/100)
img = '#' * count
nul = ' ' * (self.ImgLong - count)
if self.percent < 10:
sys.stdout.write(img + nul + " " + str(self.percent) + '%')
if self.percent >= 10 and self.percent < 100:
sys.stdout.write(img + nul + " " +str(self.percent) + '%')
if self.percent == 100:
sys.stdout.write(img + nul + '100%')
sys.stdout.write('\r')
sys.stdout.flush()
self.percent = i