自定义你的tqdm
在使用tqdm时会不会遇到打印信息,就一直滚动输出的烦恼,
现在给出一个强迫症狂喜的方案(本人轻微强迫症)
为了让进度条不懂,每次显示的都是当前处理的日志信息
可以这样做
from tqdm import tqdm as _tqdm
from collections import deque
import sysclass tqdm(_tqdm):_buffer = deque(maxlen = 20)@classmethoddef write(cls, *args, sep=" ", end="\n", file=None):msg = sep.join(map(str, args)) + endcls._buffer.append(msg)@classmethoddef reflush(cls, file=None):# 清掉之前打印的日志sys.stdout.write("\033c")sys.stdout.flush()# 重新打印缓冲区for msg in cls._buffer:_tqdm.write(msg, file=file)cls._buffer.clear()
日志输出使用
即可,用法和print函数类似
tqdm.write()
在每轮的最后刷新信息
tqdm.reflush()