Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tqdm: update total without resetting time elapsed

Tags:

python

tqdm

I'm using tqdm as I recurse over a directory tree. I don't know the number of paths I'll be using, and I don't want to build that list before I do the work just to get an accurate total count, I'd rather have it just update the progress bar as it goes along.

I've found that I can use 'reset(total=new_total)' just fine, but that also resets the time. Is there a way I can keep the time but just set the total to something new?

like image 227
Eddie Parker Avatar asked Dec 14 '22 10:12

Eddie Parker


1 Answers

Here is the definition of reset function definition inside tqdm package:

    def reset(self, total=None):
        """
        Resets to 0 iterations for repeated use.

        Consider combining with `leave=True`.

        Parameters
        ----------
        total  : int, optional. Total to use for the new bar.
        """
        self.last_print_n = self.n = 0
        self.last_print_t = self.start_t = self._time()
        if total is not None:
            self.total = total
        self.refresh()

What you need is not to update values of self.last_print_t, and self.start_t and just update the total

Instead of calling t.reset(total=new_total), you should do the following:

t.total = new_total
t.refresh()
like image 127
Vishal Avatar answered Dec 29 '22 16:12

Vishal