tqdm format remaining time

Question:

I’m running a very long process, and iterating by

    with tqdm(total=N) as pbar:
        time.sleep(1)
        pbar.update(1)

displays something like

  0%| | 528912/1.1579208923731618e+77 [00:05<320918211271131291051900907686223146304413317191111137850058393514584:44:48, 100226.38it/s 

[Quite a big combinatorial process, I’m dealing with :S ]

I will certainly try to optimize it and decrease the search-space (which I think I really cannot), but anyway, I’m curious whether if the 320918211271131291051900907686223146304413317191111137850058393514584 number of hours could be expressed as number of years + remaining days + remaining hours + remaining minutes + remaning seconds.

Any idea on how can this be achieved?

I certainly love tqdm, but it doesn’t seem easy to customize.

Thanks in advance!

Asked By: glezo

||

Answers:

It’s been a while, but for the sake of completiness, here it goes:

class TqdmExtraFormat(tqdm):
    @property
    def format_dict(self):
        d               =   super(TqdmExtraFormat, self).format_dict
        rate            =   d["rate"]
        remaining_secs  =   (d["total"] - d["n"]) / rate if rate and d["total"] else 0
        my_remaining    =   seconds_to_time_string(remaining_secs)
        d.update(my_remaining=(my_remaining))
        return d
b           =   '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{my_remaining}, {rate_fmt}{postfix}]'
jobs_list   =   range(1,1000000)
for i in TqdmExtraFormat(jobs_list, bar_format=b):
    time.sleep(0.01)
Answered By: glezo

I like to use humanize for this purpose https://pypi.org/project/humanize/

import humanize
humanize.naturaldelta(39191835)

-> '1 year, 2 months'
Answered By: Alex
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.