DEV Community

Discussion on: 🐍 Logging loop progress like a pro in Python

Collapse
 
sebvercammen profile image
Sébastien Vercammen • Edited

tqdm is a good recommendation. A different example from a project I worked on recently, using tqdm to measure continuous progress (without concrete end) and real-time rate per second, hope it helps:

from tqdm import tqdm

pbar = tqdm(desc='total update ticks', unit='tick')

def update():
    pbar.update(1)
    some_object = ...
    tqdm.write('Some text here with an object: {}'.format(some_object))

def main():
    while True:
        update()

^ will show log entries, with a final line (example w/ 15400 updates completed in 1h30min):

total update ticks: 15400tick [1:30:00, 2.85tick/s]

Bonus points if you use termcolor as well, e.g.:

from tqdm import tqdm
from termcolor import colored

tqdm.write(colored('Something bad happened', 'red'))
tqdm.write(colored('Something semi-bad happened', 'yellow'))
tqdm.write(colored('All good', 'green'))
tqdm.write(colored('Magic in the terminal', 'yellow', attrs=['bold', 'blink', 'underline']))
Collapse
 
daolf profile image
Pierre

Very nice snippets, I'm copying those in my note block.

Thank you !!