diff options
author | Munyoki Kilyungi | 2023-10-26 17:50:59 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 13:45:32 +0300 |
commit | d7451101b1a6a1bc0564ab5c9c4a23c54308926c (patch) | |
tree | 2f692b484d361695d04faa96ba3d9c84293e76f6 /gn3/db | |
parent | 6aaba6ea102264a146d47cc086bafad099d54262 (diff) | |
download | genenetwork3-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.py | 13 |
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")) |