aboutsummaryrefslogtreecommitdiff
path: root/gn3/db
diff options
context:
space:
mode:
authorMunyoki Kilyungi2023-10-26 17:50:59 +0300
committerBonfaceKilz2023-10-27 13:45:32 +0300
commitd7451101b1a6a1bc0564ab5c9c4a23c54308926c (patch)
tree2f692b484d361695d04faa96ba3d9c84293e76f6 /gn3/db
parent6aaba6ea102264a146d47cc086bafad099d54262 (diff)
downloadgenenetwork3-d7451101b1a6a1bc0564ab5c9c4a23c54308926c.tar.gz
Abstract out (sparql) CONSTRUCT into a function.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/db')
-rw-r--r--gn3/db/rdf.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py
index 54458ac..017e687 100644
--- a/gn3/db/rdf.py
+++ b/gn3/db/rdf.py
@@ -3,6 +3,11 @@
This module is a collection of functions that handle SPARQL queries.
"""
+import json
+
+from SPARQLWrapper import SPARQLWrapper
+
+
PREFIXES = {
"dcat": "http://www.w3.org/ns/dcat#",
"dct": "http://purl.org/dc/terms/",
@@ -30,3 +35,11 @@ PREFIXES = {
RDF_PREFIXES = "\n".join([f"PREFIX {key}: <{value}>"for key, value in PREFIXES.items()])
+
+
+def sparql_construct_query(query: str, endpoint: str) -> dict:
+ """Query virtuoso using a CONSTRUCT query and return a json-ld dictionary"""
+ sparql = SPARQLWrapper(endpoint)
+ sparql.setQuery(query)
+ results = sparql.queryAndConvert()
+ return json.loads(results.serialize(format="json-ld"))