tqdm模块显示进度条;输出日志时设置tqdm进度单行输出

tqdm模块显示进度条

from tqdm import tqdm
import time, random

all_list = [1,2,3,4,5,6,7,8]

with tqdm(total=100) as p_bar:
    for item in all_list:
        time.sleep(random.random())
        p_bar.update(100/(len(all_list)))
        p_bar.set_description("Processing {}-th iteration".format(i+1))

单行显示与多行显示

''' 1. 单行显示 —— 加上p_bar.close()'''
with tqdm(total=100) as p_bar:
    for item in all_list:
        time.sleep(random.random())
        p_bar.update(100/(len(all_list)))
        p_bar.set_description("Processing {}-th iteration".format(i+1))
    p_bar.close()

''' 2. 多行显示 —— 不加p_bar.close() '''
with tqdm(total=100) as p_bar:
    for item in all_list:
        time.sleep(random.random())
        p_bar.update(100/(len(all_list)))
        p_bar.set_description("Processing {}-th iteration".format(i+1))

输出日志时设置tqdm进度单行输出

  1. logger/tqdm_logger.py文件
"""
A Utility to redirect tqdm progress bar to a log file instead of stdout.
"""

__version__      = '1.0.1'
__author__       = 'Adarsh Anand'
__author_email__ = 'adarsh.anand15@gmail.com'
__license__      = 'GNU General Public License v3.0'
__url__          = 'https://github.com/adarsh-anand15/tqdm_logger'


import io
import os
import sys

__all__ = ['TqdmLogger']

class TqdmLogger(io.StringIO):
    """
        Output stream for tqdm which will output to log file instead of
        the stdout. 
        
        It is necessary to reset the logger stream everytime before starting a new tqdm progress bar.
    """

    def __init__(self, log_file, bar_stdout_flag = True):
        """
        Initialize with a log file to use for logging, 
        a buf to catch logs, and a bar index that stores the line number of the bar in file, 
        initialized to -1 to indicate that progress bar has not started yet.
        
        Args:
            log_file (str): Path of the file to write logs into

        """

        super(TqdmLogger, self).__init__()
        self.logger = log_file
        self.buf = ''
        self.bar_index = -1
        self.bar_stdout_flag = bar_stdout_flag

        # Create the log file if it doesn't already exists
        if not os.path.isfile(self.logger):
            fstream = open(self.logger, 'w', encoding='utf-8')
            fstream.close()

        
    def reset(self):
        """
        Reset the bar index to -1 before starting a new progress bar
        to avoid updating an earlier progress bar.
        """

        self.bar_index = -1
        self.buf = ''
        
    def write(self, buf, log_type = None):
        """
        Capture the buf to be written incase of progress bar else flush the buf to log file.
        """
        
        if buf.strip() and (log_type == 'text' or ('it/s' not in buf.strip() and 's/it' not in buf.strip())):
            with open(self.logger, 'a', encoding='utf-8') as ofile:
                ofile.write(buf)
                ofile.flush()
            sys.stdout.write(buf.rstrip() + '\n')
            sys.stdout.flush()
        else:
            self.buf = buf.strip('\r\n\t ')
            if self.bar_stdout_flag:
                sys.stdout.write(buf)
                sys.stdout.flush()
    
    def flush(self):
        """
        Update the progress bar. It does that by reading the entire file content, replacing the line
        containing the progress bar, and writes back everything.
        """

        #skip if buffer is empty
        if not self.buf.strip():
            return
        sys.stdout.flush()
          
        # Read the entire content of the log file
        ifstream = open(self.logger, 'r', encoding='utf-8')
        lines = ifstream.readlines()
        ifstream.close()
        
        # Replace the progress bar line with new value
        if self.bar_index != -1:
            del lines[self.bar_index]
        lines.append(self.buf.strip())
        self.bar_index = len(lines) - 1
            
        for i, line in enumerate(lines):
            lines[i] = line.strip() + '\n'
        
        # write back everything
        ofstream = open(self.logger, 'w', encoding='utf-8')
        ofstream.writelines(lines)
        ofstream.close()
        
        # reset the buffer for next cycle
        self.buf = ''	
  1. logger/logger.py文件
import logging
import os
modelPath = os.path.dirname(os.path.realpath(__file__))
logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
logger.setLevel(level=logging.DEBUG)
handler = logging.FileHandler(modelPath+"/result.log", encoding='utf-8', mode='a')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)


from logger.tqdm_logger import TqdmLogger
print('modelPath: ', modelPath)
tqdm_stream = TqdmLogger(modelPath+'/result.log')
  1. main.py调用
from logger.logger import logger, tqdm_stream
print = logger.info		# 输出重定向到日志

tqdm_stream.reset()
pbar = tqdm(total=len(all_data), file = tqdm_stream)
for each_data in all_data:
    ...
    pbar.update(1)
    
pbar.close()
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值