From 3f0757e117ed2ca1171bbf84b61793f353d67282 Mon Sep 17 00:00:00 2001 From: Sergey Astanin Date: Wed, 22 Jun 2022 18:57:16 +0200 Subject: [PATCH] fix FutureWarning about SEPARATING_LINE --- README.md | 2 +- tabulate.py | 18 ++++++++++++------ test/test_output.py | 3 ++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 767b690..ba65384 100644 --- a/README.md +++ b/README.md @@ -984,7 +984,7 @@ All versions can then be easily installed with something like: Don't forget to change your `PATH` so that `tox` knows how to find all the installed versions. Something like - export PATH="${PATH}:${HOME}/.pyenv/shims" + export PATH="${PATH}:${HOME}/.pyenv/shims" To test only some Python environments, use `-e` option. For example, to test only against Python 3.7 and Python 3.10, run: diff --git a/tabulate.py b/tabulate.py index 3aff5f9..a611658 100644 --- a/tabulate.py +++ b/tabulate.py @@ -98,6 +98,15 @@ def _is_file(f): ) +def _is_separating_line(row): + row_type = type(row) + is_sl = (row_type == list or row_type == str) and ( + (len(row) >= 1 and row[0] == SEPARATING_LINE) + or (len(row) >= 2 and row[1] == SEPARATING_LINE) + ) + return is_sl + + def _pipe_segment_with_colons(align, colwidth): """Return a segment of a horizontal line with optional colons which indicate column's alignment (as in `pipe` output format).""" @@ -1100,8 +1109,7 @@ def _remove_separating_lines(rows): separating_lines = [] sans_rows = [] for index, row in enumerate(rows): - row_type = type(row) - if (row_type == list or row_type == str) and row[0] == SEPARATING_LINE: + if _is_separating_line(row): separating_lines.append(index) else: sans_rows.append(row) @@ -1307,7 +1315,7 @@ def _normalize_tabular_data(tabular_data, headers, showindex="default"): headers = list(map(str, headers)) # rows = list(map(list, rows)) - rows = list(map(lambda r: r if r == SEPARATING_LINE else list(r), rows)) + rows = list(map(lambda r: r if _is_separating_line(r) else list(r), rows)) # add or remove an index column showindex_is_a_str = type(showindex) in [str, bytes] @@ -2139,9 +2147,7 @@ def _format_table(fmt, headers, rows, colwidths, colaligns, is_multiline, rowali for row in padded_rows: # test to see if either the 1st column or the 2nd column (account for showindex) has # the SEPARATING_LINE flag - if row[0].strip() == SEPARATING_LINE or ( - len(row) > 1 and row[1].strip() == SEPARATING_LINE - ): + if _is_separating_line(row): _append_line(lines, padded_widths, colaligns, separating_line) else: append_row(lines, row, padded_widths, colaligns, fmt.datarow) diff --git a/test/test_output.py b/test/test_output.py index a9f6f6a..82ecfb7 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -109,6 +109,7 @@ def test_plain_maxcolwidth_autowraps(): ) assert_equal(expected, result) + def test_plain_maxcolwidth_autowraps_with_sep(): "Output: maxcolwidth will result in autowrapping longer cells and separating line" table = [ @@ -1142,7 +1143,7 @@ def test_fancy_grid_multiline_row_align(): result = tabulate(table, tablefmt="fancy_grid", rowalign=[None, "center", "bottom"]) assert_equal(expected, result) - + def test_outline(): "Output: outline with headers" expected = "\n".join(