From 90774d36a4c93030c94dfbbe83af0f4e09fbe031 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 7 Nov 2019 16:57:41 +0100 Subject: [PATCH 1/3] Allow hiding progressbar until some time interval has elapsed. This avoids printing the progressbar for very short loops. --- tests/tests_tqdm.py | 15 +++++++++++++++ tqdm/std.py | 41 ++++++++++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/tests/tests_tqdm.py b/tests/tests_tqdm.py index 56033fef..5a744dd1 100644 --- a/tests/tests_tqdm.py +++ b/tests/tests_tqdm.py @@ -622,6 +622,21 @@ def test_max_interval(): t2.close() +def test_waituntil(): + """Test waituntil""" + timer = DiscreteTimer() + with closing(StringIO()) as our_file: + t = tqdm(total=2, file=our_file, leave=True, waituntil=3) + cpu_timify(t, timer) + timer.sleep(2) + t.update(1) + assert not our_file.getvalue() + timer.sleep(2) + t.update(1) + assert our_file.getvalue() + t.close() + + def test_min_iters(): """Test miniters""" with closing(StringIO()) as our_file: diff --git a/tqdm/std.py b/tqdm/std.py index e73f4ab6..6cbfd6cf 100644 --- a/tqdm/std.py +++ b/tqdm/std.py @@ -841,7 +841,7 @@ class tqdm(Comparable): def __init__(self, iterable=None, desc=None, total=None, leave=True, file=None, ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None, - ascii=None, disable=False, unit='it', unit_scale=False, + waituntil=0.0, ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000, write_bytes=None, lock_args=None, nrows=None, colour=None, gui=False, **kwargs): @@ -890,6 +890,9 @@ class tqdm(Comparable): Tweak this and `mininterval` to get very efficient loops. If your progress is erratic with both fast and slow iterations (network, skipping items, etc) you should set miniters=1. + waituntil : float, optional + Don't display the progressbar until this number [default: 0] of + seconds has elapsed. ascii : bool or str, optional If unspecified or False, use unicode (smooth blocks) to fill the meter. The fallback is to use ASCII characters " 123456789#". @@ -1061,6 +1064,7 @@ class tqdm(Comparable): self.maxinterval = maxinterval self.miniters = miniters self.dynamic_miniters = dynamic_miniters + self.waituntil = waituntil self.ascii = ascii self.disable = disable self.unit = unit @@ -1099,7 +1103,8 @@ class tqdm(Comparable): if not gui: # Initialize the screen printer self.sp = self.status_printer(self.fp) - self.refresh(lock_args=self.lock_args) + if waituntil <= 0: + self.refresh(lock_args=self.lock_args) # Init the time counter self.last_print_t = self._time() @@ -1163,6 +1168,7 @@ class tqdm(Comparable): mininterval = self.mininterval last_print_t = self.last_print_t last_print_n = self.last_print_n + start_t = self.start_t n = self.n time = self._time @@ -1174,8 +1180,9 @@ class tqdm(Comparable): n += 1 if n - last_print_n >= self.miniters: - dt = time() - last_print_t - if dt >= mininterval: + cur_t = time() + dt = cur_t - last_print_t + if dt >= mininterval and cur_t - start_t >= self.waituntil: self.update(n - last_print_n) last_print_n = self.last_print_n last_print_t = self.last_print_t @@ -1218,8 +1225,10 @@ class tqdm(Comparable): # check counter first to reduce calls to time() if self.n - self.last_print_n >= self.miniters: - dt = self._time() - self.last_print_t - if dt >= self.mininterval: + cur_t = self._time() + dt = cur_t - self.last_print_t + if (dt >= self.mininterval + and cur_t - self.start_t >= self.waituntil): cur_t = self._time() dn = self.n - self.last_print_n # >= n if self.smoothing and dt and dn: @@ -1279,15 +1288,17 @@ class tqdm(Comparable): leave = pos == 0 if self.leave is None else self.leave with self._lock: - if leave: - # stats for overall rate (no weighted average) - self._ema_dt = lambda: None - self.display(pos=0) - fp_write('\n') - else: - # clear previous display - if self.display(msg='', pos=pos) and not pos: - fp_write('\r') + # If the following doesn't hold, we haven't even printed anything. + if self.last_print_t - self.start_t >= self.waituntil: + if leave: + # stats for overall rate (no weighted average) + self._ema_dt = lambda: None + self.display(pos=0) + fp_write('\n') + else: + # clear previous display + if self.display(msg='', pos=pos) and not pos: + fp_write('\r') def clear(self, nolock=False): """Clear current bar display.""" From caf6dbbaae721f94009846f5bdd8bd1431cdde28 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Tue, 24 Mar 2020 21:34:20 +0000 Subject: [PATCH 2/3] performance/optimisation and slight tidy --- tqdm/std.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/tqdm/std.py b/tqdm/std.py index 6cbfd6cf..4730c915 100644 --- a/tqdm/std.py +++ b/tqdm/std.py @@ -841,10 +841,11 @@ class tqdm(Comparable): def __init__(self, iterable=None, desc=None, total=None, leave=True, file=None, ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None, - waituntil=0.0, ascii=None, disable=False, unit='it', unit_scale=False, + ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000, write_bytes=None, - lock_args=None, nrows=None, colour=None, gui=False, **kwargs): + lock_args=None, nrows=None, colour=None, waituntil=0, gui=False, + **kwargs): """ Parameters ---------- @@ -890,9 +891,6 @@ class tqdm(Comparable): Tweak this and `mininterval` to get very efficient loops. If your progress is erratic with both fast and slow iterations (network, skipping items, etc) you should set miniters=1. - waituntil : float, optional - Don't display the progressbar until this number [default: 0] of - seconds has elapsed. ascii : bool or str, optional If unspecified or False, use unicode (smooth blocks) to fill the meter. The fallback is to use ASCII characters " 123456789#". @@ -954,6 +952,8 @@ class tqdm(Comparable): The fallback is 20. colour : str, optional Bar colour (e.g. 'green', '#00ff00'). + waituntil : float, optional + Don't display until [default: 0] seconds have elapsed. gui : bool, optional WARNING: internal parameter - do not use. Use tqdm.gui.tqdm(...) instead. If set, will attempt to use @@ -1064,7 +1064,6 @@ class tqdm(Comparable): self.maxinterval = maxinterval self.miniters = miniters self.dynamic_miniters = dynamic_miniters - self.waituntil = waituntil self.ascii = ascii self.disable = disable self.unit = unit @@ -1072,6 +1071,7 @@ class tqdm(Comparable): self.unit_divisor = unit_divisor self.initial = initial self.lock_args = lock_args + self.waituntil = waituntil self.gui = gui self.dynamic_ncols = dynamic_ncols self.smoothing = smoothing @@ -1168,7 +1168,7 @@ class tqdm(Comparable): mininterval = self.mininterval last_print_t = self.last_print_t last_print_n = self.last_print_n - start_t = self.start_t + min_start_t = self.start_t + self.waituntil n = self.n time = self._time @@ -1182,7 +1182,7 @@ class tqdm(Comparable): if n - last_print_n >= self.miniters: cur_t = time() dt = cur_t - last_print_t - if dt >= mininterval and cur_t - start_t >= self.waituntil: + if dt >= mininterval and cur_t >= min_start_t: self.update(n - last_print_n) last_print_n = self.last_print_n last_print_t = self.last_print_t @@ -1227,8 +1227,7 @@ class tqdm(Comparable): if self.n - self.last_print_n >= self.miniters: cur_t = self._time() dt = cur_t - self.last_print_t - if (dt >= self.mininterval - and cur_t - self.start_t >= self.waituntil): + if dt >= self.mininterval and cur_t >= self.start_t + self.waituntil: cur_t = self._time() dn = self.n - self.last_print_n # >= n if self.smoothing and dt and dn: @@ -1288,17 +1287,18 @@ class tqdm(Comparable): leave = pos == 0 if self.leave is None else self.leave with self._lock: - # If the following doesn't hold, we haven't even printed anything. - if self.last_print_t - self.start_t >= self.waituntil: - if leave: - # stats for overall rate (no weighted average) - self._ema_dt = lambda: None - self.display(pos=0) - fp_write('\n') - else: - # clear previous display - if self.display(msg='', pos=pos) and not pos: - fp_write('\r') + if self.last_print_t < self.start_t + self.waituntil: + # haven't ever displayed; nothing to clear + return + if leave: + # stats for overall rate (no weighted average) + self._ema_dt = lambda: None + self.display(pos=0) + fp_write('\n') + else: + # clear previous display + if self.display(msg='', pos=pos) and not pos: + fp_write('\r') def clear(self, nolock=False): """Clear current bar display.""" From 90cb341e8eba094042237e6c65a8754066c174ce Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Thu, 25 Feb 2021 16:08:12 +0000 Subject: [PATCH 3/3] rename `waituntil` => `delay` --- tests/tests_tqdm.py | 6 +++--- tqdm/std.py | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/tests_tqdm.py b/tests/tests_tqdm.py index 5a744dd1..8cf00653 100644 --- a/tests/tests_tqdm.py +++ b/tests/tests_tqdm.py @@ -622,11 +622,11 @@ def test_max_interval(): t2.close() -def test_waituntil(): - """Test waituntil""" +def test_delay(): + """Test delay""" timer = DiscreteTimer() with closing(StringIO()) as our_file: - t = tqdm(total=2, file=our_file, leave=True, waituntil=3) + t = tqdm(total=2, file=our_file, leave=True, delay=3) cpu_timify(t, timer) timer.sleep(2) t.update(1) diff --git a/tqdm/std.py b/tqdm/std.py index 4730c915..bc5e71ec 100644 --- a/tqdm/std.py +++ b/tqdm/std.py @@ -844,7 +844,7 @@ class tqdm(Comparable): ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000, write_bytes=None, - lock_args=None, nrows=None, colour=None, waituntil=0, gui=False, + lock_args=None, nrows=None, colour=None, delay=0, gui=False, **kwargs): """ Parameters @@ -952,7 +952,7 @@ class tqdm(Comparable): The fallback is 20. colour : str, optional Bar colour (e.g. 'green', '#00ff00'). - waituntil : float, optional + delay : float, optional Don't display until [default: 0] seconds have elapsed. gui : bool, optional WARNING: internal parameter - do not use. @@ -1071,7 +1071,7 @@ class tqdm(Comparable): self.unit_divisor = unit_divisor self.initial = initial self.lock_args = lock_args - self.waituntil = waituntil + self.delay = delay self.gui = gui self.dynamic_ncols = dynamic_ncols self.smoothing = smoothing @@ -1103,7 +1103,7 @@ class tqdm(Comparable): if not gui: # Initialize the screen printer self.sp = self.status_printer(self.fp) - if waituntil <= 0: + if delay <= 0: self.refresh(lock_args=self.lock_args) # Init the time counter @@ -1168,7 +1168,7 @@ class tqdm(Comparable): mininterval = self.mininterval last_print_t = self.last_print_t last_print_n = self.last_print_n - min_start_t = self.start_t + self.waituntil + min_start_t = self.start_t + self.delay n = self.n time = self._time @@ -1227,7 +1227,7 @@ class tqdm(Comparable): if self.n - self.last_print_n >= self.miniters: cur_t = self._time() dt = cur_t - self.last_print_t - if dt >= self.mininterval and cur_t >= self.start_t + self.waituntil: + if dt >= self.mininterval and cur_t >= self.start_t + self.delay: cur_t = self._time() dn = self.n - self.last_print_n # >= n if self.smoothing and dt and dn: @@ -1287,7 +1287,7 @@ class tqdm(Comparable): leave = pos == 0 if self.leave is None else self.leave with self._lock: - if self.last_print_t < self.start_t + self.waituntil: + if self.last_print_t < self.start_t + self.delay: # haven't ever displayed; nothing to clear return if leave: