Skip to content

Commit c76ace0

Browse files
committed
Merge branch 'case_sparql_select_add_json_output' into case_sparql_select_cross_json_and_anno_flag_testing
2 parents b1bcad3 + e699592 commit c76ace0

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

case_utils/case_sparql_select/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ def graph_and_query_to_data_frame(
123123
def data_frame_to_table_text(
124124
df: pd.DataFrame,
125125
*args: typing.Any,
126+
json_indent: typing.Optional[int] = None,
127+
json_orient: str,
126128
output_mode: str,
127129
use_header: bool,
128130
use_index: bool,
@@ -160,6 +162,12 @@ def data_frame_to_table_text(
160162
table_text = df.to_html(
161163
classes=("table", "table-bordered", "table-condensed"), **general_kwargs
162164
)
165+
elif output_mode == "json":
166+
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html
167+
168+
table_text = df.to_json(
169+
indent=json_indent, orient=json_orient, date_format="iso"
170+
)
163171
elif output_mode == "md":
164172
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_markdown.html
165173
# https://pypi.org/project/tabulate/
@@ -199,14 +207,25 @@ def main() -> None:
199207
action="store_true",
200208
help="Raise error if no results are returned for query.",
201209
)
210+
parser.add_argument(
211+
"--json-indent",
212+
type=int,
213+
help="Number of whitespace characters to use for indentation. Only applicable for JSON output.",
214+
)
215+
parser.add_argument(
216+
"--json-orient",
217+
default="columns",
218+
choices=("columns", "index", "records", "split", "table", "values"),
219+
help="Orientation to use for Pandas DataFrame JSON output. Only applicable for JSON output.",
220+
)
202221
parser.add_argument(
203222
"--use-prefixes",
204223
action="store_true",
205224
help="Abbreviate node IDs according to graph's encoded prefixes. (This will use prefixes in the graph, not the query.)",
206225
)
207226
parser.add_argument(
208227
"out_table",
209-
help="Expected extensions are .html for HTML tables, .md for Markdown tables, .csv for comma-separated values, and .tsv for tab-separated values.",
228+
help="Expected extensions are .html for HTML tables, .json for JSON tables, .md for Markdown tables, .csv for comma-separated values, and .tsv for tab-separated values. Note that JSON is a Pandas output JSON format (chosen by '--json-orient'), and not JSON-LD.",
210229
)
211230
parser.add_argument(
212231
"in_sparql",
@@ -293,6 +312,8 @@ def main() -> None:
293312

294313
table_text = data_frame_to_table_text(
295314
df,
315+
json_indent=args.json_indent,
316+
json_orient=args.json_orient,
296317
output_mode=output_mode,
297318
use_header=use_header,
298319
use_index=use_index,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"?nFile":{
3+
"0":"kb:file-1",
4+
"1":"kb:file-2"
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"?nFile":{"0":"kb:file-1","1":"kb:file-2"}}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"?name":{
3+
"0":"Johnny Lee Outlaw",
4+
"1":"Peter Goodguy"
5+
},
6+
"?mbox":{
7+
"0":"mailto:[email protected]",
8+
"1":"mailto:[email protected]"
9+
}
10+
}
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:[email protected]","1":"mailto:[email protected]"}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
prefixed*
22
subclass-*.md
3-
w3-output.*
3+
w3-output*

tests/case_utils/case_sparql_select/Makefile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ tests_srcdir := $(top_srcdir)/tests
2020
all: \
2121
prefixed_results.csv \
2222
prefixed_results.html \
23+
prefixed_results.json \
24+
prefixed_results-indented.json \
2325
prefixed_results.md \
2426
prefixed_results.tsv \
2527
subclass-explicit-none.md \
2628
subclass-implicit-any.md \
2729
w3-output.csv \
2830
w3-output.html \
31+
w3-output.json \
32+
w3-output-indented.json \
2933
w3-output.md \
3034
w3-output.tsv
3135

@@ -35,6 +39,8 @@ all: \
3539
check-subclass-implicit-any \
3640
check-w3-csv \
3741
check-w3-html \
42+
check-w3-json \
43+
check-w3-json-indented \
3844
check-w3-markdown \
3945
check-w3-tsv
4046

@@ -46,6 +52,7 @@ all: \
4652
check: \
4753
check-w3-csv \
4854
check-w3-html \
55+
check-w3-json \
4956
check-w3-markdown \
5057
check-w3-tsv \
5158
check-prefixed_results \
@@ -57,6 +64,7 @@ check: \
5764
check-prefixed_results: \
5865
check-prefixed_results-csv \
5966
check-prefixed_results-html \
67+
check-prefixed_results-json \
6068
check-prefixed_results-md \
6169
check-prefixed_results-tsv
6270

@@ -70,6 +78,19 @@ check-prefixed_results-html: \
7078
prefixed_results.html
7179
diff $^
7280

81+
check-prefixed_results-json: \
82+
check-prefixed_results-json-indented \
83+
.check-prefixed_results.json \
84+
prefixed_results.json
85+
diff \
86+
.check-prefixed_results.json \
87+
prefixed_results.json
88+
89+
check-prefixed_results-json-indented: \
90+
.check-prefixed_results-indented.json \
91+
prefixed_results-indented.json
92+
diff $^
93+
7394
check-prefixed_results-md: \
7495
.check-prefixed_results.md \
7596
prefixed_results.md
@@ -104,6 +125,19 @@ check-w3-html: \
104125
w3-output.html
105126
diff $^
106127

128+
check-w3-json: \
129+
.check-w3-output.json \
130+
check-w3-json-indented \
131+
w3-output.json
132+
diff \
133+
.check-w3-output.json \
134+
w3-output.json
135+
136+
check-w3-json-indented: \
137+
.check-w3-output-indented.json \
138+
w3-output-indented.json
139+
diff $^
140+
107141
check-w3-markdown: \
108142
.check-w3-output.md \
109143
w3-output.md
@@ -122,6 +156,7 @@ clean:
122156
*.html \
123157
*.md \
124158
*.tsv \
159+
*output*.json \
125160
_*
126161

127162
prefixed_results.%: \
@@ -139,6 +174,22 @@ prefixed_results.%: \
139174
subclass.json
140175
mv _$@ $@
141176

177+
prefixed_results-indented.json: \
178+
$(tests_srcdir)/.venv.done.log \
179+
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
180+
$(top_srcdir)/case_utils/ontology/__init__.py \
181+
$(top_srcdir)/case_utils/ontology/version_info.py \
182+
subclass.json \
183+
subclass.sparql
184+
source $(tests_srcdir)/venv/bin/activate \
185+
&& case_sparql_select \
186+
--json-indent 4 \
187+
--use-prefixes \
188+
_$@ \
189+
subclass.sparql \
190+
subclass.json
191+
mv _$@ $@
192+
142193
subclass-explicit-none.md: \
143194
$(tests_srcdir)/.venv.done.log \
144195
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
@@ -183,3 +234,20 @@ w3-output.%: \
183234
w3-input-2.ttl \
184235
w3-input-3.json
185236
mv _$@ $@
237+
238+
w3-output-indented.json: \
239+
$(tests_srcdir)/.venv.done.log \
240+
$(top_srcdir)/case_utils/case_sparql_select/__init__.py \
241+
$(top_srcdir)/case_utils/ontology/__init__.py \
242+
$(top_srcdir)/case_utils/ontology/version_info.py \
243+
w3-input-1.sparql \
244+
w3-input-2.ttl \
245+
w3-input-3.json
246+
source $(tests_srcdir)/venv/bin/activate \
247+
&& case_sparql_select \
248+
--json-indent 4 \
249+
_$@ \
250+
w3-input-1.sparql \
251+
w3-input-2.ttl \
252+
w3-input-3.json
253+
mv _$@ $@

0 commit comments

Comments
 (0)