From 422b36ec1191f07ff0816a0e3701ee3874afdb96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Tue, 23 Feb 2021 19:27:21 +0100 Subject: [PATCH 1/4] Replace constant PRESERVE_WHITESPACE with an argument to tabulate() --- tabulate.py | 17 ++++++++--------- test/test_output.py | 6 ++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/tabulate.py b/tabulate.py index 55b896e..541cf29 100644 --- a/tabulate.py +++ b/tabulate.py @@ -68,9 +68,6 @@ def _is_file(f): # minimum extra space in headers MIN_PADDING = 2 -# Whether or not to preserve leading/trailing whitespace in data. -PRESERVE_WHITESPACE = False - _DEFAULT_FLOATFMT = "g" _DEFAULT_MISSINGVAL = "" # default align will be overwritten by "left", "center" or "decimal" @@ -810,13 +807,13 @@ def _choose_width_fn(has_invisible, enable_widechars, is_multiline): return width_fn -def _align_column_choose_padfn(strings, alignment, has_invisible): +def _align_column_choose_padfn(strings, alignment, has_invisible, preserve_whitespace): if alignment == "right": - if not PRESERVE_WHITESPACE: + if not preserve_whitespace: strings = [s.strip() for s in strings] padfn = _padleft elif alignment == "center": - if not PRESERVE_WHITESPACE: + if not preserve_whitespace: strings = [s.strip() for s in strings] padfn = _padboth elif alignment == "decimal": @@ -830,7 +827,7 @@ def _align_column_choose_padfn(strings, alignment, has_invisible): elif not alignment: padfn = _padnone else: - if not PRESERVE_WHITESPACE: + if not preserve_whitespace: strings = [s.strip() for s in strings] padfn = _padright return strings, padfn @@ -873,9 +870,10 @@ def _align_column( has_invisible=True, enable_widechars=False, is_multiline=False, + preserve_whitespace=False ): """[string] -> [padded_string]""" - strings, padfn = _align_column_choose_padfn(strings, alignment, has_invisible) + strings, padfn = _align_column_choose_padfn(strings, alignment, has_invisible, preserve_whitespace) width_fn = _align_column_choose_width_fn( has_invisible, enable_widechars, is_multiline ) @@ -1224,6 +1222,7 @@ def tabulate( showindex="default", disable_numparse=False, colalign=None, + preserve_whitespace=False ): """Format a fixed width table for pretty printing. @@ -1603,7 +1602,7 @@ def tabulate( [width_fn(h) + min_padding for h in headers] if headers else [0] * len(cols) ) cols = [ - _align_column(c, a, minw, has_invisible, enable_widechars, is_multiline) + _align_column(c, a, minw, has_invisible, enable_widechars, is_multiline, preserve_whitespace) for c, a, minw in zip(cols, aligns, minwidths) ] diff --git a/test/test_output.py b/test/test_output.py index abab439..660f047 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -1598,18 +1598,16 @@ def test_disable_numparse_list(): def test_preserve_whitespace(): "Output: Default table output, but with preserved leading whitespace." - tabulate_module.PRESERVE_WHITESPACE = True table_headers = ["h1", "h2", "h3"] test_table = [[" foo", " bar ", "foo"]] expected = "\n".join( ["h1 h2 h3", "----- ------- ----", " foo bar foo"] ) - result = tabulate(test_table, table_headers) + result = tabulate(test_table, table_headers, preserve_whitespace=True) assert_equal(expected, result) - tabulate_module.PRESERVE_WHITESPACE = False table_headers = ["h1", "h2", "h3"] test_table = [[" foo", " bar ", "foo"]] expected = "\n".join(["h1 h2 h3", "---- ---- ----", "foo bar foo"]) - result = tabulate(test_table, table_headers) + result = tabulate(test_table, table_headers, preserve_whitespace=False) assert_equal(expected, result) From 049d7d9701d70a0b05c405b574f0b69925ac51fa Mon Sep 17 00:00:00 2001 From: Sergey Astanin Date: Thu, 26 Sep 2024 18:31:44 +0200 Subject: [PATCH 2/4] apply black to the PR branch and fix lint errors --- README.md | 2 +- tabulate/__init__.py | 42 ++++++++++++++++++++++++++++++++--------- test/common.py | 2 ++ test/test_output.py | 16 ++++++++++++---- test/test_regression.py | 1 + 5 files changed, 49 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b596cf5..52c7644 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ When data is a list of dictionaries, a dictionary can be passed as `headers` to replace the keys with other column labels: ```pycon ->>> print(tabulate([{1: "Alice", 2: 24}, {1: "Bob", 2: 19}], +>>> print(tabulate([{1: "Alice", 2: 24}, {1: "Bob", 2: 19}], ... headers={1: "Name", 2: "Age"})) Name Age ------ ----- diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 63d8d32..598cb4e 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -160,7 +160,6 @@ def _grid_line_with_colons(colwidths, colaligns): return "+" + "+".join(segments) + "+" - def _mediawiki_row_with_attrs(separator, cell_values, colwidths, colaligns): alignment = { "left": "", @@ -1160,10 +1159,12 @@ def _align_column( has_invisible=True, enable_widechars=False, is_multiline=False, - preserve_whitespace=False + preserve_whitespace=False, ): """[string] -> [padded_string]""" - strings, padfn = _align_column_choose_padfn(strings, alignment, has_invisible, preserve_whitespace) + strings, padfn = _align_column_choose_padfn( + strings, alignment, has_invisible, preserve_whitespace + ) width_fn = _align_column_choose_width_fn( has_invisible, enable_widechars, is_multiline ) @@ -1269,15 +1270,21 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True): return f"{val}" elif valtype is int: if isinstance(val, str): - val_striped = val.encode('unicode_escape').decode('utf-8') - colored = re.search(r'(\\[xX]+[0-9a-fA-F]+\[\d+[mM]+)([0-9.]+)(\\.*)$', val_striped) + val_striped = val.encode("unicode_escape").decode("utf-8") + colored = re.search( + r"(\\[xX]+[0-9a-fA-F]+\[\d+[mM]+)([0-9.]+)(\\.*)$", val_striped + ) if colored: total_groups = len(colored.groups()) if total_groups == 3: digits = colored.group(2) if digits.isdigit(): - val_new = colored.group(1) + format(int(digits), intfmt) + colored.group(3) - val = val_new.encode('utf-8').decode('unicode_escape') + val_new = ( + colored.group(1) + + format(int(digits), intfmt) + + colored.group(3) + ) + val = val_new.encode("utf-8").decode("unicode_escape") intfmt = "" return format(val, intfmt) elif valtype is bytes: @@ -2322,7 +2329,15 @@ def tabulate( if tablefmt == "colon_grid": aligns_copy = ["left"] * len(cols) cols = [ - _align_column(c, a, minw, has_invisible, enable_widechars, is_multiline, preserve_whitespace) + _align_column( + c, + a, + minw, + has_invisible, + enable_widechars, + is_multiline, + preserve_whitespace, + ) for c, a, minw in zip(cols, aligns_copy, minwidths) ] @@ -2819,7 +2834,16 @@ def _main(): opts, args = getopt.getopt( sys.argv[1:], "h1o:s:F:I:f:", - ["help", "header", "output=", "sep=", "float=", "int=", "colalign=", "format="], + [ + "help", + "header", + "output=", + "sep=", + "float=", + "int=", + "colalign=", + "format=", + ], ) except getopt.GetoptError as e: print(e) diff --git a/test/common.py b/test/common.py index ca5a069..fe33259 100644 --- a/test/common.py +++ b/test/common.py @@ -2,11 +2,13 @@ from pytest import skip, raises # noqa import warnings + def assert_equal(expected, result): print("Expected:\n%s\n" % expected) print("Got:\n%s\n" % result) assert expected == result + def assert_in(result, expected_set): nums = range(1, len(expected_set) + 1) for i, expected in zip(nums, expected_set): diff --git a/test/test_output.py b/test/test_output.py index c90f90c..059742c 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -1,7 +1,6 @@ """Test output of the various forms of tabular data.""" from pytest import mark -import tabulate as tabulate_module from common import assert_equal, raises, skip, check_warnings from tabulate import tabulate, simple_separated_format, SEPARATING_LINE @@ -1459,7 +1458,12 @@ def test_colon_grid(): "+------+------+", ] ) - result = tabulate([[3, 4]], headers=("H1", "H2"), tablefmt="colon_grid", colalign=["right", "center"]) + result = tabulate( + [[3, 4]], + headers=("H1", "H2"), + tablefmt="colon_grid", + colalign=["right", "center"], + ) assert_equal(expected, result) @@ -1482,7 +1486,9 @@ def test_colon_grid_wide_characters(): "+-----------+---------+", ] ) - result = tabulate(_test_table, headers, tablefmt="colon_grid", colalign=["left", "right"]) + result = tabulate( + _test_table, headers, tablefmt="colon_grid", colalign=["left", "right"] + ) assert_equal(expected, result) @@ -2773,7 +2779,9 @@ def test_intfmt_with_string_as_integer(): @mark.skip(reason="It detects all values as floats but there are strings and integers.") def test_intfmt_with_string_with_floats(): "Output: integer format" - result = tabulate([[82000.38], ["1500.47"], ["2463"], [92165]], intfmt=",", tablefmt="plain") + result = tabulate( + [[82000.38], ["1500.47"], ["2463"], [92165]], intfmt=",", tablefmt="plain" + ) expected = "82000.4\n 1500.47\n 2463\n92,165" assert_equal(expected, result) diff --git a/test/test_regression.py b/test/test_regression.py index d2fc3a0..5289db0 100644 --- a/test/test_regression.py +++ b/test/test_regression.py @@ -513,6 +513,7 @@ def test_numpy_int64_as_integer(): except ImportError: raise skip("") + def test_empty_table_with_colalign(): "Regression: empty table with colalign kwarg" table = tabulate([], ["a", "b", "c"], colalign=("center", "left", "left", "center")) From 54ac10757ac23a5b9bd4f483f35730a5d9dea437 Mon Sep 17 00:00:00 2001 From: Sergey Astanin Date: Thu, 26 Sep 2024 18:33:50 +0200 Subject: [PATCH 3/4] add preserve_whitespace to test_tabulate_signature --- test/test_api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_api.py b/test/test_api.py index e658e82..062573c 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -50,6 +50,7 @@ def test_tabulate_signature(): ("disable_numparse", False), ("colglobalalign", None), ("colalign", None), + ("preserve_whitespace", False), ("maxcolwidths", None), ("headersglobalalign", None), ("headersalign", None), From 0d5d30e81a98caed4fc867e76ec5ca10cb5ff20f Mon Sep 17 00:00:00 2001 From: Sergey Astanin Date: Thu, 26 Sep 2024 18:38:36 +0200 Subject: [PATCH 4/4] update README according to the new preserve_whitespace usage --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 52c7644..089f20e 100644 --- a/README.md +++ b/README.md @@ -739,13 +739,8 @@ column, in which case every column may have different number formatting: ### Text formatting By default, `tabulate` removes leading and trailing whitespace from text -columns. To disable whitespace removal, set the global module-level flag -`PRESERVE_WHITESPACE`: - -```python -import tabulate -tabulate.PRESERVE_WHITESPACE = True -``` +columns. To disable whitespace removal, pass `preserve_whitespace=True`. +Older versions of the library used a global module-level flag PRESERVE_WHITESPACE. ### Wide (fullwidth CJK) symbols