Last active
August 29, 2018 06:16
-
-
Save rileypeterson/3d1879d9adc0b3809a70181e4573d1b5 to your computer and use it in GitHub Desktop.
Progress Bar for Logging WIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import time | |
import numpy as np | |
def prog_log(logger, percentage, pwidth=20, pchar="#"): | |
""" Take a logger object and percentage and output a | |
progress bar to the log | |
""" | |
original_format = logger.handlers[0].formatter | |
logger.info("Progress:") | |
logger.handlers[0].setFormatter(None) | |
logger.handlers[0].terminator = "" | |
logger.info(pwidth*"."+"|"+"\r") | |
bins = np.linspace(0, 100, pwidth) | |
_width = bins[1] | |
old_prog = "" | |
# Last thing is to push for loop outside | |
# Break into a function that just returns a string given a percentage | |
# | |
for percentage in np.linspace(0, 100, 33): | |
# print(percentage, bins) | |
# print(len(np.where(percentage > bins))) | |
if percentage == 100: | |
logger.info("\r"+pwidth*pchar+"| "+format(float(percentage),".2f")+" %") | |
break | |
binz = np.where(percentage > bins)[0] | |
char_progress = len(binz) * pchar | |
val = str((percentage % _width)/_width).split('.')[1][0] | |
if old_prog != char_progress: | |
old_prog = char_progress | |
logger.info("\r"+char_progress+((pwidth-len(char_progress))*".")+"| "+format(float(percentage),".2f")+" %") | |
else: | |
logger.info("\r"+char_progress+val+((pwidth-len(char_progress)-1)*".")+"| "+format(float(percentage),".2f")+" %") | |
time.sleep(1) | |
logger.info('\n') | |
logger.handlers[0].setFormatter(original_format) | |
logger.handlers[0].terminator = "\n" | |
return logger, percentage | |
def make_log(file): | |
logger = logging.getLogger(__name__) | |
fh = logging.StreamHandler() | |
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') | |
fh.setFormatter(formatter) | |
logger.addHandler(fh) | |
logger.setLevel(logging.DEBUG) | |
return logger, fh | |
logger, fh = make_log('log.log') | |
prog_log(logger, 100) | |
logger.info("FOO") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment