Skip to content

Commit ed44477

Browse files
committed
Merge JSON output and --no-(header,index) flag branches
This patch addresses issues with some parameter-values being compatible with some of the DataFrame table rendering functions, but not with certain `orient` parameters of the JSON rendering function. The incompatible pairings are now caught at parameter-parsing time. Signed-off-by: Alex Nelson <alexander.nelson@nist.gov>
1 parent c76ace0 commit ed44477

18 files changed

+58
-7
lines changed

case_utils/case_sparql_select/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,11 @@ def data_frame_to_table_text(
165165
elif output_mode == "json":
166166
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html
167167

168+
# Drop unsupported kwarg.
169+
del general_kwargs["header"]
170+
168171
table_text = df.to_json(
169-
indent=json_indent, orient=json_orient, date_format="iso"
172+
indent=json_indent, orient=json_orient, date_format="iso", **general_kwargs
170173
)
171174
elif output_mode == "md":
172175
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_markdown.html
@@ -253,7 +256,7 @@ def main() -> None:
253256
parser_index_group.add_argument(
254257
"--no-index",
255258
action="store_true",
256-
help="Do not print index.",
259+
help="Do not print index. If output is JSON, --json-orient must be 'split' or 'table'.",
257260
)
258261

259262
parser.add_argument("in_graph", nargs="+")
@@ -302,6 +305,15 @@ def main() -> None:
302305
else:
303306
use_index = True
304307

308+
if (
309+
output_mode == "json"
310+
and use_index is False
311+
and args.json_orient not in {"split", "table"}
312+
):
313+
raise ValueError(
314+
"For JSON output, --no-index flag requires --json-orient to be either 'split' or 'table'."
315+
)
316+
305317
df = graph_and_query_to_data_frame(
306318
graph,
307319
select_query_text,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"?name":{"0":"Johnny Lee Outlaw","1":"Peter Goodguy"},"?mbox":{"0":"mailto:jlow@example.com","1":"mailto:peter@example.org"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"0":{"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},"1":{"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},{"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"columns":["?name","?mbox"],"index":[0,1],"data":[["Johnny Lee Outlaw","mailto:jlow@example.com"],["Peter Goodguy","mailto:peter@example.org"]]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"schema":{"fields":[{"name":"index","type":"integer"},{"name":"?name","type":"string"},{"name":"?mbox","type":"string"}],"primaryKey":["index"],"pandas_version":"1.4.0"},"data":[{"index":0,"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},{"index":1,"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["Johnny Lee Outlaw","mailto:jlow@example.com"],["Peter Goodguy","mailto:peter@example.org"]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"columns":["?name","?mbox"],"data":[["Johnny Lee Outlaw","mailto:jlow@example.com"],["Peter Goodguy","mailto:peter@example.org"]]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"schema":{"fields":[{"name":"?name","type":"string"},{"name":"?mbox","type":"string"}],"pandas_version":"1.4.0"},"data":[{"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},{"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"?name":{"0":"Johnny Lee Outlaw","1":"Peter Goodguy"},"?mbox":{"0":"mailto:jlow@example.com","1":"mailto:peter@example.org"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"0":{"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},"1":{"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},{"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"columns":["?name","?mbox"],"index":[0,1],"data":[["Johnny Lee Outlaw","mailto:jlow@example.com"],["Peter Goodguy","mailto:peter@example.org"]]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"schema":{"fields":[{"name":"index","type":"integer"},{"name":"?name","type":"string"},{"name":"?mbox","type":"string"}],"primaryKey":["index"],"pandas_version":"1.4.0"},"data":[{"index":0,"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},{"index":1,"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["Johnny Lee Outlaw","mailto:jlow@example.com"],["Peter Goodguy","mailto:peter@example.org"]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"columns":["?name","?mbox"],"data":[["Johnny Lee Outlaw","mailto:jlow@example.com"],["Peter Goodguy","mailto:peter@example.org"]]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"schema":{"fields":[{"name":"?name","type":"string"},{"name":"?mbox","type":"string"}],"pandas_version":"1.4.0"},"data":[{"?name":"Johnny Lee Outlaw","?mbox":"mailto:jlow@example.com"},{"?name":"Peter Goodguy","?mbox":"mailto:peter@example.org"}]}

tests/case_utils/case_sparql_select/test_data_frame_to_table_text_json.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,59 @@
3737

3838

3939
def make_data_frame_to_json_table_text_parameters() -> typing.Iterator[
40-
typing.Tuple[str, bool, bool]
40+
typing.Tuple[str, str, bool, bool]
4141
]:
4242
for use_header in [False, True]:
4343
for use_index in [False, True]:
44-
for output_mode in ["csv", "html", "md", "tsv"]:
45-
yield (output_mode, use_header, use_index)
44+
for output_mode in ["csv", "html", "json", "md", "tsv"]:
45+
if output_mode == "json":
46+
for json_orient in [
47+
"columns",
48+
"index",
49+
"records",
50+
"split",
51+
"table",
52+
"values",
53+
]:
54+
# Handle incompatible parameter pairings for JSON mode.
55+
if use_index is False:
56+
if json_orient not in {"split", "table"}:
57+
continue
58+
59+
yield (json_orient, output_mode, use_header, use_index)
60+
else:
61+
yield ("columns", output_mode, use_header, use_index)
4662

4763

4864
@pytest.mark.parametrize(
49-
"output_mode, use_header, use_index",
65+
"json_orient, output_mode, use_header, use_index",
5066
make_data_frame_to_json_table_text_parameters(),
5167
)
5268
def test_data_frame_to_table_text_json(
69+
json_orient: str,
5370
output_mode: str,
5471
use_header: bool,
5572
use_index: bool,
5673
) -> None:
5774
table_text = case_utils.case_sparql_select.data_frame_to_table_text(
5875
DATA_FRAME,
76+
json_orient=json_orient,
5977
output_mode=output_mode,
6078
use_header=use_header,
6179
use_index=use_index,
6280
)
6381

64-
output_filename_template = ".check-w3-output-%s_header-%s_index.%s"
82+
output_filename_template = ".check-w3-output-%s_header-%s_index%s.%s"
6583
header_part = "with" if use_header else "without"
6684
index_part = "with" if use_index else "without"
85+
if output_mode == "json":
86+
json_orient_part = "-orient-" + json_orient
87+
else:
88+
json_orient_part = ""
6789
output_filename = output_filename_template % (
6890
header_part,
6991
index_part,
92+
json_orient_part,
7093
output_mode,
7194
)
7295
with (SRCDIR / output_filename).open("w") as out_fh:

0 commit comments

Comments
 (0)