Archive

Posts Tagged ‘Arabia’

a command line progress bar for your loops

January 10, 2014 Leave a comment

Problem
When I was working on Project Euler, there were several problems that I solved with a brute force approach and thus the runtime was several hours. To see some changes and to know approximately when it finishes, I added a simple counter to the main loop that showed the progress in percentage.

Is there a simpler way to add a progress bar to a loop?

Solution
The project tqdm addresses this problem. Reddit discussion is here.

Usage example:

import time
from tqdm import tqdm

def main():
    for i in tqdm(range(10000)):
        time.sleep(.005)

Notes:

  • you can use xrange too: tqdm(xrange(10000))
  • you can write trange: trange(10000)

On the github page of the project you will find an animated gif too.

Categories: python Tags: , , ,