def progress(index0,rowmax0,msg='',skip=0,refresh_time=1,bar_len=20): """ 展示进度条的自定义函数,接收6个参数 index0:当前的进度序号 rowmax0:总进度序号 msg:进度条附带显示的信息 skip:进度条从什么序号开始,默认是0,适用于中断后从断点继续工作的场合 refresh_time:多长时间刷新一次进度条,默认是1秒 bar_len:进度条长度,默认是20 """ global T global T0 try: cd_time=time.time()-T except: T0=time.time() T=time.time() cd_time=time.time()-T index=index0-skip index=1 if index==0 else index rowmax=rowmax0-skip if cd_time>refresh_time: T=time.time() progress=index/rowmax*100 starNum=int(bar_len*progress/100) minusNum=min(bar_len-starNum,bar_len) gap=max(time.time()-T0,0.001) remain=gap/index*(rowmax-index) d=remain/86400 h=remain/3600 m=remain/60 s=remain #left if d>2: left=d unit2='d' elif h>2: left=h unit2='h' elif m>10: left=m unit2='m' else: left=s unit2='s' #speed if gap/index>1: unit1='id' if gap/index<60: speed=gap/index unit0='s' elif gap/index<3600: speed=gap/index/60 unit0='m' else: speed=gap/index/3600 unit0='h' else: unit1='s' if index/gap<1000: speed=index/gap unit0='r' elif index/gap<1000000: speed=index/gap/1000 unit0='kr' else: speed=index/gap/1000000 unit0='mr' print('\r||%s%s|| %d/%d(%d), %.4f%%, %.2f%s left, %.2f %s/%s, %s.'%( '#'*starNum,'-'*minusNum,index0,rowmax0,skip,progress,left,unit2,speed,unit0,unit1,msg),end='') if index==rowmax: gap=max(time.time()-T0,0.001) T=None T0=None d=gap//86400 h=gap%86400//3600 m=gap%3600//60 s=gap%60 if gap/index>1: unit1='id' if gap/index<60: speed=gap/index unit0='s' elif gap/index<3600: speed=gap/index/60 unit0='m' else: speed=gap/index/3600 unit0='h' else: unit1='s' if index/gap<1000: speed=index/gap unit0='r' elif index/gap<1000000: speed=index/gap/1000 unit0='kr' else: speed=index/gap/1000000 unit0='mr' print('\rfinished, %dd %dh %dm %ds comsumed, %.2f %s/%s.'%(d,h,m,s,speed,unit0,unit1),end='') def progressReset(): global T global T0 T=time.time() T0=time.time() def progressEnd(): global T global T0 if T0==None: return None gap=time.time()-T0 T=None T0=None d=gap//86400 h=gap%86400//3600 m=gap%3600//60 s=gap%60 print('\nfinished, %dd %dh %dm %ds consumed.'%(d,h,m,s))
进度条的效果如下所示
||#####################-|| 245 out of 247, 99.1903%, 0d 0h 0m 0s remain..