Skip to content

Commit 303a042

Browse files
author
Stefan Sullivan
committed
flake8 and black formatting changes
1 parent c928342 commit 303a042

File tree

4 files changed

+143
-80
lines changed

4 files changed

+143
-80
lines changed

tabulate/__init__.py

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,24 @@ class TableFormat:
9292
- or a list of elements not to be displayed if the table has column headers.
9393
9494
"""
95-
def __init__(self,
96-
lineabove, linebelowheader, linebetweenrows, linebelow, headerrow, datarow, padding, with_header_hide,
97-
min_padding=MIN_PADDING,
98-
disable_numparse=None,
99-
numalign="decimal",
100-
stralign="left",
101-
multiline=True,
102-
escape_first_column=None,
103-
):
95+
96+
def __init__(
97+
self,
98+
lineabove,
99+
linebelowheader,
100+
linebetweenrows,
101+
linebelow,
102+
headerrow,
103+
datarow,
104+
padding,
105+
with_header_hide,
106+
min_padding=MIN_PADDING,
107+
disable_numparse=None,
108+
numalign="decimal",
109+
stralign="left",
110+
multiline=True,
111+
escape_first_column=None,
112+
):
104113
# tuple fields
105114
self.lineabove = lineabove
106115
self.linebelowheader = linebelowheader
@@ -308,7 +317,7 @@ def escape_char(c):
308317
return _build_simple_row(escaped_values, rowfmt)
309318

310319

311-
def _rst_escape_first_column(rows, headers, escape_value='..'):
320+
def _rst_escape_first_column(rows, headers, escape_value=".."):
312321
def escape_empty(val):
313322
if isinstance(val, (str, bytes)) and not val.strip():
314323
return escape_value
@@ -572,7 +581,7 @@ def escape_empty(val):
572581
datarow=DataRow("", " ", ""),
573582
padding=0,
574583
with_header_hide=None,
575-
escape_first_column='..',
584+
escape_first_column="..",
576585
),
577586
"mediawiki": TableFormat(
578587
lineabove=Line(
@@ -643,7 +652,7 @@ def escape_empty(val):
643652
datarow=_latex_row,
644653
padding=1,
645654
with_header_hide=None,
646-
multiline=False
655+
multiline=False,
647656
),
648657
"latex_raw": TableFormat(
649658
lineabove=_latex_line_begin_tabular,
@@ -830,7 +839,7 @@ def simple_separated_format(separator, **kwargs):
830839
datarow=DataRow("", separator, ""),
831840
padding=0,
832841
with_header_hide=None,
833-
**kwargs
842+
**kwargs,
834843
)
835844

836845

@@ -1308,7 +1317,7 @@ def _align_header(
13081317

13091318

13101319
def _remove_separating_lines(rows):
1311-
if type(rows) == list:
1320+
if isinstance(rows, list):
13121321
separating_lines = []
13131322
sans_rows = []
13141323
for index, row in enumerate(rows):
@@ -1356,7 +1365,8 @@ def _bool(val):
13561365

13571366

13581367
def _normalize_tabular_data(tabular_data, headers, showindex="default"):
1359-
"""Transform a supported data type to a list of lists, and a list of headers, with headers padding.
1368+
"""Transform a supported data type to a list of lists, and a list of headers, with headers
1369+
padding.
13601370
13611371
Supported tabular data types:
13621372
@@ -2162,9 +2172,15 @@ def tabulate(
21622172
# empty values in the first column of RST tables should be escaped (issue #82)
21632173
# "" should be escaped as "\\ " or ".."
21642174
if tablefmt.escape_first_column is not None:
2165-
list_of_lists, headers = _rst_escape_first_column(list_of_lists, headers, tablefmt.escape_first_column)
2175+
list_of_lists, headers = _rst_escape_first_column(
2176+
list_of_lists, headers, tablefmt.escape_first_column
2177+
)
21662178

2167-
disable_numparse = tablefmt.disable_numparse if tablefmt.disable_numparse is not None else disable_numparse
2179+
disable_numparse = (
2180+
tablefmt.disable_numparse
2181+
if tablefmt.disable_numparse is not None
2182+
else disable_numparse
2183+
)
21682184
numalign = tablefmt.numalign if numalign == _DEFAULT_ALIGN else numalign
21692185
stralign = tablefmt.stralign if stralign == _DEFAULT_ALIGN else stralign
21702186

@@ -2223,22 +2239,28 @@ def tabulate(
22232239

22242240
# align columns
22252241
# first set global alignment
2226-
if colglobalalign is not None: # if global alignment provided
2242+
if colglobalalign is not None: # if global alignment provided
22272243
aligns = [colglobalalign] * len(cols)
2228-
else: # default
2244+
else: # default
22292245
aligns = [numalign if ct in [int, float] else stralign for ct in coltypes]
22302246
# then specific alignements
22312247
if colalign is not None:
22322248
assert isinstance(colalign, Iterable)
22332249
if isinstance(colalign, str):
2234-
warnings.warn(f"As a string, `colalign` is interpreted as {[c for c in colalign]}. Did you mean `colglobalalign = \"{colalign}\"` or `colalign = (\"{colalign}\",)`?", stacklevel=2)
2250+
warnings.warn(
2251+
f"As a string, `colalign` is interpreted as {[c for c in colalign]}. "
2252+
f'Did you mean `colglobalalign = "{colalign}"` or `colalign = ("{colalign}",)`?',
2253+
stacklevel=2,
2254+
)
22352255
for idx, align in enumerate(colalign):
22362256
if not idx < len(aligns):
22372257
break
22382258
elif align != "global":
22392259
aligns[idx] = align
22402260
minwidths = (
2241-
[width_fn(h) + tablefmt.min_padding for h in headers] if headers else [0] * len(cols)
2261+
[width_fn(h) + tablefmt.min_padding for h in headers]
2262+
if headers
2263+
else [0] * len(cols)
22422264
)
22432265
cols = [
22442266
_align_column(c, a, minw, has_invisible, enable_widechars, tablefmt.multiline)
@@ -2250,20 +2272,25 @@ def tabulate(
22502272
# align headers and add headers
22512273
t_cols = cols or [[""]] * len(headers)
22522274
# first set global alignment
2253-
if headersglobalalign is not None: # if global alignment provided
2275+
if headersglobalalign is not None: # if global alignment provided
22542276
aligns_headers = [headersglobalalign] * len(t_cols)
2255-
else: # default
2277+
else: # default
22562278
aligns_headers = aligns or [stralign] * len(headers)
22572279
# then specific header alignements
22582280
if headersalign is not None:
22592281
assert isinstance(headersalign, Iterable)
22602282
if isinstance(headersalign, str):
2261-
warnings.warn(f"As a string, `headersalign` is interpreted as {[c for c in headersalign]}. Did you mean `headersglobalalign = \"{headersalign}\"` or `headersalign = (\"{headersalign}\",)`?", stacklevel=2)
2283+
warnings.warn(
2284+
f"As a string, `headersalign` is interpreted as {[c for c in headersalign]}. "
2285+
f'Did you mean `headersglobalalign = "{headersalign}"` '
2286+
f'or `headersalign = ("{headersalign}",)`?',
2287+
stacklevel=2,
2288+
)
22622289
for idx, align in enumerate(headersalign):
22632290
hidx = headers_pad + idx
22642291
if not hidx < len(aligns_headers):
22652292
break
2266-
elif align == "same" and hidx < len(aligns): # same as column align
2293+
elif align == "same" and hidx < len(aligns): # same as column align
22672294
aligns_headers[hidx] = aligns[hidx]
22682295
elif align != "global":
22692296
aligns_headers[hidx] = align
@@ -2285,7 +2312,14 @@ def tabulate(
22852312
_reinsert_separating_lines(rows, separating_lines)
22862313

22872314
return _format_table(
2288-
tablefmt, headers, aligns_headers, rows, minwidths, aligns, tablefmt.multiline, rowaligns=rowaligns
2315+
tablefmt,
2316+
headers,
2317+
aligns_headers,
2318+
rows,
2319+
minwidths,
2320+
aligns,
2321+
tablefmt.multiline,
2322+
rowaligns=rowaligns,
22892323
)
22902324

22912325

@@ -2416,7 +2450,9 @@ def str(self):
24162450
return self
24172451

24182452

2419-
def _format_table(fmt, headers, headersaligns, rows, colwidths, colaligns, is_multiline, rowaligns):
2453+
def _format_table(
2454+
fmt, headers, headersaligns, rows, colwidths, colaligns, is_multiline, rowaligns
2455+
):
24202456
"""Produce a plain-text representation of the table."""
24212457
lines = []
24222458
hidden = fmt.with_header_hide if (headers and fmt.with_header_hide) else []

test/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def rows_to_pipe_table_str(rows):
1515

1616
return "\n".join(lines)
1717

18+
1819
def check_warnings(func_args_kwargs, *, num=None, category=None, contain=None):
1920
func, args, kwargs = func_args_kwargs
2021
with warnings.catch_warnings(record=True) as W:
@@ -28,4 +29,3 @@ def check_warnings(func_args_kwargs, *, num=None, category=None, contain=None):
2829
assert all([issubclass(w.category, category) for w in W])
2930
if contain is not None:
3031
assert all([contain in str(w.message) for w in W])
31-

test/test_cli.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def sample_input(sep=" ", with_headers=False):
7777

7878

7979
def run_and_capture_stdout(cmd, input=None):
80-
return subprocess.run(cmd, input=input, check=True, capture_output=True, encoding='utf-8').stdout
80+
proc = subprocess.run(
81+
cmd, input=input, check=True, capture_output=True, encoding="utf-8"
82+
)
83+
return proc.stdout
8184

8285

8386
class TemporaryTextFile:

test/test_output.py

Lines changed: 75 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -244,21 +244,33 @@ def test_simple():
244244

245245

246246
def test_simple_with_zero_padding():
247-
""" Output custom simple table built with a new column separator """
248-
expected = textwrap.dedent("""\
247+
"""Output custom simple table built with a new column separator"""
248+
expected = textwrap.dedent(
249+
"""\
249250
strings numbers
250-
eggs 451""")
251-
result = tabulate([_test_table[-1]], _test_table_headers, tablefmt=simple_separated_format(" ", min_padding=0))
251+
eggs 451"""
252+
)
253+
result = tabulate(
254+
[_test_table[-1]],
255+
_test_table_headers,
256+
tablefmt=simple_separated_format(" ", min_padding=0),
257+
)
252258
assert result == expected
253259

254260

255261
def test_simple_with_zero_padding_and_alignment():
256-
""" Output custom simple table built with a new column separator """
257-
expected = textwrap.dedent("""\
262+
"""Output custom simple table built with a new column separator"""
263+
expected = textwrap.dedent(
264+
"""\
258265
strings numbers
259266
spam 41.9999
260-
eggs 451""")
261-
result = tabulate(_test_table, _test_table_headers, tablefmt=simple_separated_format(" ", min_padding=0))
267+
eggs 451"""
268+
)
269+
result = tabulate(
270+
_test_table,
271+
_test_table_headers,
272+
tablefmt=simple_separated_format(" ", min_padding=0),
273+
)
262274
assert result == expected
263275

264276

@@ -2700,60 +2712,72 @@ def test_colalign_multi_with_sep_line():
27002712
expected = " one two\n\nthree four"
27012713
assert expected == result
27022714

2715+
27032716
def test_column_global_and_specific_alignment():
2704-
""" Test `colglobalalign` and `"global"` parameter for `colalign`. """
2705-
table = [[1,2,3,4],[111,222,333,444]]
2706-
colglobalalign = 'center'
2707-
colalign = ('global','left', 'right')
2717+
"""Test `colglobalalign` and `"global"` parameter for `colalign`."""
2718+
table = [[1, 2, 3, 4], [111, 222, 333, 444]]
2719+
colglobalalign = "center"
2720+
colalign = ("global", "left", "right")
27082721
result = tabulate(table, colglobalalign=colglobalalign, colalign=colalign)
2709-
expected = '\n'.join([
2710-
"--- --- --- ---",
2711-
" 1 2 3 4",
2712-
"111 222 333 444",
2713-
"--- --- --- ---"])
2722+
expected = "\n".join(
2723+
[
2724+
"--- --- --- ---",
2725+
" 1 2 3 4",
2726+
"111 222 333 444",
2727+
"--- --- --- ---",
2728+
]
2729+
)
27142730
assert expected == result
27152731

2732+
27162733
def test_headers_global_and_specific_alignment():
2717-
""" Test `headersglobalalign` and `headersalign`. """
2718-
table = [[1,2,3,4,5,6],[111,222,333,444,555,666]]
2719-
colglobalalign = 'center'
2720-
colalign = ('left',)
2721-
headers = ['h', 'e', 'a', 'd', 'e', 'r']
2722-
headersglobalalign = 'right'
2723-
headersalign = ('same', 'same', 'left', 'global', 'center')
2724-
result = tabulate(table, headers=headers, colglobalalign=colglobalalign, colalign=colalign, headersglobalalign=headersglobalalign, headersalign=headersalign)
2725-
expected = '\n'.join([
2726-
"h e a d e r",
2727-
"--- --- --- --- --- ---",
2728-
"1 2 3 4 5 6",
2729-
"111 222 333 444 555 666"])
2734+
"""Test `headersglobalalign` and `headersalign`."""
2735+
table = [[1, 2, 3, 4, 5, 6], [111, 222, 333, 444, 555, 666]]
2736+
colglobalalign = "center"
2737+
colalign = ("left",)
2738+
headers = ["h", "e", "a", "d", "e", "r"]
2739+
headersglobalalign = "right"
2740+
headersalign = ("same", "same", "left", "global", "center")
2741+
result = tabulate(
2742+
table,
2743+
headers=headers,
2744+
colglobalalign=colglobalalign,
2745+
colalign=colalign,
2746+
headersglobalalign=headersglobalalign,
2747+
headersalign=headersalign,
2748+
)
2749+
expected = "\n".join(
2750+
[
2751+
"h e a d e r",
2752+
"--- --- --- --- --- ---",
2753+
"1 2 3 4 5 6",
2754+
"111 222 333 444 555 666",
2755+
]
2756+
)
27302757
assert expected == result
27312758

2759+
27322760
def test_colalign_or_headersalign_too_long():
2733-
""" Test `colalign` and `headersalign` too long. """
2734-
table = [[1,2],[111,222]]
2735-
colalign = ('global', 'left', 'center')
2736-
headers = ['h']
2737-
headersalign = ('center', 'right', 'same')
2738-
result = tabulate(table, headers=headers, colalign=colalign, headersalign=headersalign)
2739-
expected = '\n'.join([
2740-
" h",
2741-
"--- ---",
2742-
" 1 2",
2743-
"111 222"])
2761+
"""Test `colalign` and `headersalign` too long."""
2762+
table = [[1, 2], [111, 222]]
2763+
colalign = ("global", "left", "center")
2764+
headers = ["h"]
2765+
headersalign = ("center", "right", "same")
2766+
result = tabulate(
2767+
table, headers=headers, colalign=colalign, headersalign=headersalign
2768+
)
2769+
expected = "\n".join([" h", "--- ---", " 1 2", "111 222"])
27442770
assert expected == result
27452771

2772+
27462773
def test_warning_when_colalign_or_headersalign_is_string():
2747-
""" Test user warnings when `colalign` or `headersalign` is a string. """
2748-
table = [[1,"bar"]]
2749-
opt = {
2750-
'colalign': "center",
2751-
'headers': ['foo', '2'],
2752-
'headersalign': "center"}
2753-
check_warnings((tabulate, [table], opt),
2754-
num = 2,
2755-
category = UserWarning,
2756-
contain = "As a string")
2774+
"""Test user warnings when `colalign` or `headersalign` is a string."""
2775+
table = [[1, "bar"]]
2776+
opt = {"colalign": "center", "headers": ["foo", "2"], "headersalign": "center"}
2777+
check_warnings(
2778+
(tabulate, [table], opt), num=2, category=UserWarning, contain="As a string"
2779+
)
2780+
27572781

27582782
def test_float_conversions():
27592783
"Output: float format parsed"

0 commit comments

Comments
 (0)