Skip to content

Commit cf15727

Browse files
committed
ruff format & ruff check
1 parent 17bf1cf commit cf15727

4 files changed

Lines changed: 47 additions & 30 deletions

File tree

examples/people.jsonl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{"id": 1, "name": "Alice", "email": "alice@example.com"}
2-
{"id": 2, "name": "Bob", "email": "bob@example.com"}
2+
{"id": 2, "name": "Bob", "email": "bob@example.com"}

tabulate/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,4 +2897,5 @@ def _wrap_chunks(self, chunks):
28972897

28982898
if __name__ == "__main__":
28992899
from .cli import _main
2900+
29002901
_main()

tabulate/cli.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Command-line interface for tabulate."""
22

3+
from functools import partial
34
import re
45
import sys
56
import textwrap
6-
from functools import partial
77

88
try:
99
from . import (
@@ -15,8 +15,9 @@
1515
)
1616
except ImportError: # pragma: no cover
1717
# running as a script: python tabulate/cli.py
18-
import sys as _sys
1918
import os as _os
19+
import sys as _sys
20+
2021
_sys.path.insert(0, _os.path.dirname(_os.path.dirname(_os.path.abspath(__file__))))
2122
from tabulate import (
2223
_DEFAULT_FLOATFMT,
@@ -79,8 +80,8 @@ def _main():
7980
[
8081
"help",
8182
"header", # deprecated in CLI > 0.10
82-
"headers=", # CLI > 0.10
83-
"read=", # CLI > 0.10
83+
"headers=", # CLI > 0.10
84+
"read=", # CLI > 0.10
8485
"output=",
8586
"sep=",
8687
"float=",
@@ -151,8 +152,8 @@ def _main():
151152
if type(headers) is str and headers not in special_headers_values:
152153
# "," and ":" in header titles are not supported in CLI
153154
try:
154-
headers2 = dict(tuple(hh.split(":",2)) for hh in headers.split(","))
155-
except:
155+
headers2 = dict(tuple(hh.split(":", 2)) for hh in headers.split(","))
156+
except Exception:
156157
print(f"cannot parse headers parameter: {headers}", file=sys.stderr)
157158
headers2 = []
158159
headers = headers2
@@ -165,14 +166,16 @@ def _main():
165166
for f in files:
166167
if f == "-":
167168
f = sys.stdin
168-
_open_and_pprint_file(reader, f,
169-
headers=headers,
170-
tablefmt=tablefmt,
171-
floatfmt=floatfmt,
172-
intfmt=intfmt,
173-
file=out,
174-
colalign=colalign,
175-
)
169+
_open_and_pprint_file(
170+
reader,
171+
f,
172+
headers=headers,
173+
tablefmt=tablefmt,
174+
floatfmt=floatfmt,
175+
intfmt=intfmt,
176+
file=out,
177+
colalign=colalign,
178+
)
176179

177180

178181
def _read_rsv_file(fobject, sep):
@@ -183,13 +186,15 @@ def _read_rsv_file(fobject, sep):
183186

184187
def _read_jsonl_file(fobject):
185188
import json
186-
rows:list[str] = fobject.readlines()
189+
190+
rows: list[str] = fobject.readlines()
187191
table = [json.loads(row) for row in rows]
188192
return table
189193

190194

191195
def _read_csv_file(fobject):
192196
import csv
197+
193198
reader = csv.reader(fobject, dialect="excel")
194199
table = [list(row) for row in reader]
195200
return table

test/test_cli.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import tempfile
99
from unittest.mock import patch
1010

11-
from common import assert_equal
1211
from tabulate.cli import _main
1312

13+
from common import assert_equal
14+
1415

1516
class _UnclosableStringIO(io.StringIO):
1617
"""StringIO that ignores close() so getvalue() works after a 'with' block."""
18+
1719
def close(self):
1820
pass # _main does `with sys.stdout as out:`, which would close a plain StringIO
1921

@@ -22,12 +24,15 @@ def run_main_in_process(args, input_text=None):
2224
"""Call _main() in-process, capturing stdout. Returns the captured output."""
2325
stdin = io.StringIO(input_text) if input_text is not None else sys.stdin
2426
stdout = _UnclosableStringIO()
25-
with patch("sys.argv", ["tabulate"] + args), \
26-
patch("sys.stdin", stdin), \
27-
contextlib.redirect_stdout(stdout):
27+
with (
28+
patch("sys.argv", ["tabulate"] + args),
29+
patch("sys.stdin", stdin),
30+
contextlib.redirect_stdout(stdout),
31+
):
2832
_main()
2933
return stdout.getvalue()
3034

35+
3136
SAMPLE_SIMPLE_FORMAT = "\n".join(
3237
[
3338
"----- ------ -------------",
@@ -281,7 +286,7 @@ def test_module_jsonl_from_stdin():
281286
SAMPLE_CSV_FORMAT = "\n".join(
282287
[
283288
"-- ----- ----------------- ----------------",
284-
"id name email \"favorite\" fruit",
289+
'id name email "favorite" fruit',
285290
"1 Alice alice@example.com apple, kiwi",
286291
"2 Bob bob@example.com banana,",
287292
" orange,",
@@ -291,6 +296,7 @@ def test_module_jsonl_from_stdin():
291296
]
292297
)
293298

299+
294300
def test_module_csv_from_stdin():
295301
"""Command line utility: python -m tabulate with CSV input from stdin"""
296302
cmd = [sys.executable, "-m", "tabulate", "-r", "csv"]
@@ -304,9 +310,13 @@ def test_module_csv_from_stdin():
304310
def test_module_jsonl_remapped_headers():
305311
"""Command line utility: --headers with key:header remapping for JSONL input"""
306312
cmd = [
307-
sys.executable, "-m", "tabulate",
308-
"-r", "jsonl",
309-
"--headers", "id:ID,name:First Name,email:Email",
313+
sys.executable,
314+
"-m",
315+
"tabulate",
316+
"-r",
317+
"jsonl",
318+
"--headers",
319+
"id:ID,name:First Name,email:Email",
310320
]
311321
out = run_and_capture_stdout(cmd, input=SAMPLE_INPUT_JSONL)
312322
expected = SAMPLE_REMAPPED_HEADERS
@@ -320,6 +330,7 @@ def test_module_jsonl_remapped_headers():
320330
# that coverage.py can instrument the code in tabulate/cli.py.
321331
# ---------------------------------------------------------------------------
322332

333+
323334
def test_inprocess_stdin_to_stdout():
324335
"""In-process: read RSV from stdin, print to stdout"""
325336
out = run_main_in_process([], input_text=sample_input())
@@ -390,6 +401,7 @@ def test_inprocess_csv_from_stdin():
390401
def test_inprocess_invalid_option():
391402
"""In-process: unrecognised option exits with code 2"""
392403
import pytest
404+
393405
with pytest.raises(SystemExit) as exc_info:
394406
run_main_in_process(["--no-such-option"], input_text="a b\n1 2\n")
395407
assert exc_info.value.code == 2
@@ -398,6 +410,7 @@ def test_inprocess_invalid_option():
398410
def test_inprocess_help_option():
399411
"""In-process: --help / -h exits with code 0"""
400412
import pytest
413+
401414
for opt in ["-h", "--help"]:
402415
with pytest.raises(SystemExit) as exc_info:
403416
run_main_in_process([opt], input_text="")
@@ -407,6 +420,7 @@ def test_inprocess_help_option():
407420
def test_inprocess_invalid_format():
408421
"""In-process: unknown --format value exits with code 3"""
409422
import pytest
423+
410424
with pytest.raises(SystemExit) as exc_info:
411425
run_main_in_process(["-f", "nosuchformat"], input_text="a b\n1 2\n")
412426
assert exc_info.value.code == 3
@@ -415,6 +429,7 @@ def test_inprocess_invalid_format():
415429
def test_inprocess_invalid_fileformat():
416430
"""In-process: unknown --read value exits with code 3"""
417431
import pytest
432+
418433
with pytest.raises(SystemExit) as exc_info:
419434
run_main_in_process(["-r", "xml"], input_text="")
420435
assert exc_info.value.code == 3
@@ -424,9 +439,7 @@ def test_inprocess_int_option():
424439
"""In-process: -I / --int option"""
425440
jsonl_ints = '{"n": 1000000}\n{"n": 2000000}\n'
426441
for opt in ["-I", "--int"]:
427-
out = run_main_in_process(
428-
["-r", "jsonl", opt, "_"], input_text=jsonl_ints
429-
)
442+
out = run_main_in_process(["-r", "jsonl", opt, "_"], input_text=jsonl_ints)
430443
assert "1_000_000" in out
431444

432445

@@ -441,9 +454,7 @@ def test_inprocess_colalign_option():
441454

442455
def test_inprocess_rsv_custom_headers():
443456
"""In-process: --headers with custom column names for RSV input"""
444-
out = run_main_in_process(
445-
["--headers", "Planet,Radius,Mass"], input_text=sample_input()
446-
)
457+
out = run_main_in_process(["--headers", "Planet,Radius,Mass"], input_text=sample_input())
447458
assert_equal(out.splitlines(), SAMPLE_SIMPLE_FORMAT_WITH_HEADERS.splitlines())
448459

449460

0 commit comments

Comments
 (0)