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"}}

0 commit comments

Comments
 (0)