Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tqdm show progress for a generator I know the length of

I'm looping over a large file that I know the length of, but am processing lazily since it's too large to fit in memory. I'd like to be able to use tqdm to keep track of my progress through the file, but since it can't get the total number of examples out of the generator I'm using, the only thing it shows is the estimated iterations/second. Is there any way to tell tqdm how many elements it's going to be looping over total so I can get some of the other statistics?

like image 878
George Avatar asked Feb 01 '17 17:02

George


1 Answers

You can pass the length to the argument total to make it work.

Example:

from tqdm import tqdm  length = 1000000 generator = (3 * n for n in range(length))  # just doing something random for n in tqdm(generator, total=length):     pass 
like image 58
O. Edholm Avatar answered Sep 20 '22 11:09

O. Edholm