From a7e717dc53f192e62ac2958632a82d3f7c90c3e9 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Sat, 18 Sep 2021 22:10:50 +0100 Subject: [PATCH] misc linting/refactoring --- .meta/mkdocs.py | 4 ++-- tests/tests_contrib.py | 4 ++-- tests/tests_perf.py | 11 ++++------ tests/tests_tqdm.py | 46 +++++++++++++++--------------------------- tqdm/std.py | 8 +++----- 5 files changed, 27 insertions(+), 46 deletions(-) diff --git a/.meta/mkdocs.py b/.meta/mkdocs.py index b86697f0..40a9f71d 100644 --- a/.meta/mkdocs.py +++ b/.meta/mkdocs.py @@ -39,8 +39,8 @@ def doc2rst(doc, arglist=True, raw=False): else: doc = dedent(doc) if arglist: - doc = '\n'.join([i if not i or i[0] == ' ' else '* ' + i + ' ' - for i in doc.split('\n')]) + doc = '\n'.join(i if not i or i[0] == ' ' else '* ' + i + ' ' + for i in doc.split('\n')) return doc diff --git a/tests/tests_contrib.py b/tests/tests_contrib.py index 3a839625..69a1cada 100644 --- a/tests/tests_contrib.py +++ b/tests/tests_contrib.py @@ -26,10 +26,10 @@ def test_enumerate(tqdm_kwargs): enumerate(a, 42) ) with closing(StringIO()) as our_file: - _ = list(tenumerate((i for i in a), file=our_file, **tqdm_kwargs)) + _ = list(tenumerate(iter(a), file=our_file, **tqdm_kwargs)) assert "100%" not in our_file.getvalue() with closing(StringIO()) as our_file: - _ = list(tenumerate((i for i in a), file=our_file, total=len(a), **tqdm_kwargs)) + _ = list(tenumerate(iter(a), file=our_file, total=len(a), **tqdm_kwargs)) assert "100%" in our_file.getvalue() diff --git a/tests/tests_perf.py b/tests/tests_perf.py index be47fe87..552a1694 100644 --- a/tests/tests_perf.py +++ b/tests/tests_perf.py @@ -114,10 +114,7 @@ def simple_progress(iterable=None, total=None, file=sys.stdout, desc='', spent = last_t[0] - start_t[0] spent_fmt = format_interval(spent) rate = n[0] / spent if spent > 0 else 0 - if 0.0 < rate < 1.0: - rate_fmt = "%.2fs/it" % (1.0 / rate) - else: - rate_fmt = "%.2fit/s" % rate + rate_fmt = "%.2fs/it" % (1.0 / rate) if 0.0 < rate < 1.0 else "%.2fit/s" % rate frac = n[0] / total percentage = int(frac * 100) @@ -172,7 +169,7 @@ def test_iter_basic_overhead(): with relative_timer() as time_tqdm: for i in t: a += i - assert a == (total * total - total) / 2.0 + assert a == (total ** 2 - total) / 2.0 a = 0 with relative_timer() as time_bench: @@ -248,7 +245,7 @@ def test_iter_overhead_hard(): with relative_timer() as time_tqdm: for i in t: a += i - assert a == (total * total - total) / 2.0 + assert a == (total ** 2 - total) / 2.0 a = 0 with relative_timer() as time_bench: @@ -292,7 +289,7 @@ def test_iter_overhead_simplebar_hard(): with relative_timer() as time_tqdm: for i in t: a += i - assert a == (total * total - total) / 2.0 + assert a == (total ** 2 - total) / 2.0 a = 0 s = simple_progress(_range(total), leave=True, diff --git a/tests/tests_tqdm.py b/tests/tests_tqdm.py index 90ad2f22..99f57077 100644 --- a/tests/tests_tqdm.py +++ b/tests/tests_tqdm.py @@ -160,42 +160,28 @@ def progressbar_rate(bar_str): def squash_ctrlchars(s): """Apply control characters in a string just like a terminal display""" - # Init variables - curline = 0 # current line in our fake terminal - lines = [''] # state of our fake terminal - - # Split input string by control codes - s_split = RE_ctrlchr.split(s) - s_split = filter(None, s_split) # filter out empty splits - - # For each control character or message - for nextctrl in s_split: - # If it's a control character, apply it + curline = 0 + lines = [''] # state of fake terminal + for nextctrl in filter(None, RE_ctrlchr.split(s)): + # apply control chars if nextctrl == '\r': - # Carriage return - # Go to the beginning of the line - # simplified here: we just empty the string + # go to line beginning (simplified here: just empty the string) lines[curline] = '' elif nextctrl == '\n': - # Newline - # Go to the next line - if curline < (len(lines) - 1): - # If already exists, just move cursor - curline += 1 - else: - # Else the new line is created + if curline >= len(lines) - 1: + # wrap-around creates newline lines.append('') - curline += 1 + # move cursor down + curline += 1 elif nextctrl == '\x1b[A': - # Move cursor up + # move cursor up if curline > 0: curline -= 1 else: - raise ValueError("Cannot go up, anymore!") - # Else, it is a message, we print it on current line + raise ValueError("Cannot go further up") else: + # print message on current line lines[curline] += nextctrl - return lines @@ -852,12 +838,12 @@ def test_infinite_total(): def test_nototal(): """Test unknown total length""" with closing(StringIO()) as our_file: - for _ in tqdm((i for i in range(10)), file=our_file, unit_scale=10): + for _ in tqdm(iter(range(10)), file=our_file, unit_scale=10): pass assert "100it" in our_file.getvalue() with closing(StringIO()) as our_file: - for _ in tqdm((i for i in range(10)), file=our_file, + for _ in tqdm(iter(range(10)), file=our_file, bar_format="{l_bar}{bar}{r_bar}"): pass assert "10/?" in our_file.getvalue() @@ -1804,9 +1790,9 @@ def test_bool(): assert not t with tqdm([0], **kwargs) as t: assert t - with tqdm((x for x in []), **kwargs) as t: + with tqdm(iter([]), **kwargs) as t: assert t - with tqdm((x for x in [1, 2, 3]), **kwargs) as t: + with tqdm(iter([1, 2, 3]), **kwargs) as t: assert t with tqdm(**kwargs) as t: try: diff --git a/tqdm/std.py b/tqdm/std.py index 5f821b20..e81c8368 100644 --- a/tqdm/std.py +++ b/tqdm/std.py @@ -1049,7 +1049,7 @@ class tqdm(Comparable): if ascii is None: ascii = not _supports_unicode(file) - if bar_format and not ((ascii is True) or _is_ascii(ascii)): + if bar_format and ascii is not True and not _is_ascii(ascii): # Convert bar format into unicode since terminal uses unicode bar_format = _unicode(bar_format) @@ -1099,10 +1099,8 @@ class tqdm(Comparable): # if nested, at initial sp() call we replace '\r' by '\n' to # not overwrite the outer progress bar with self._lock: - if position is None: - self.pos = self._get_free_pos(self) - else: # mark fixed positions as negative - self.pos = -position + # mark fixed positions as negative + self.pos = self._get_free_pos(self) if position is None else -position if not gui: # Initialize the screen printer