Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tqdm: extract time passed + time remaining?

I have been going over the tqdm docs, but no matter where I look, I cannot find a method by which to extract the time passed and estimated time remaining fields (basically the center of the progress bar on each line: 00:00<00:02).

 0%|          | 0/200 [00:00<?, ?it/s]
  4%|▎         | 7/200 [00:00<00:02, 68.64it/s]
  8%|▊         | 16/200 [00:00<00:02, 72.87it/s]
 12%|█▎        | 25/200 [00:00<00:02, 77.15it/s]
 17%|█▋        | 34/200 [00:00<00:02, 79.79it/s]
 22%|██▏       | 43/200 [00:00<00:01, 79.91it/s]
 26%|██▌       | 52/200 [00:00<00:01, 80.23it/s]
 30%|███       | 61/200 [00:00<00:01, 82.13it/s]
....
100%|██████████| 200/200 [00:02<00:00, 81.22it/s]

tqdm works via essentially printing a dynamic progress bar anytime an update occurs, but is there a way to "just" print the 00:01 and 00:02 portions, so I could use them elsewhere in my Python program, such as in automatic stopping code that halts the process if it is taking too long?

like image 347
Coolio2654 Avatar asked Jun 19 '19 23:06

Coolio2654


2 Answers

tqdm objects expose some information via the public property format_dict.

from tqdm import tqdm

with tqdm(total=100) as t:
    ...
    t.update()
    print(t.format_interval(t.format_dict['elapsed']))

Otherwise you could parse str(t).split()

like image 157
casper.dcl Avatar answered Nov 16 '22 03:11

casper.dcl


You can get elapsed and remaining time from format_dict and some calculations.

t = tqdm(total=100)
...
elapsed = t.format_dict["elapsed"]
rate = t.format_dict["rate"]
remaining = (t.total - t.n) / rate if rate and t.total else 0  # Seconds*
like image 1
Artem Khodakov Avatar answered Nov 16 '22 01:11

Artem Khodakov