diff options
author | Munyoki Kilyungi | 2023-10-27 16:21:28 +0300 |
---|---|---|
committer | BonfaceKilz | 2023-10-27 16:55:16 +0300 |
commit | b24f7c6143dd1182819aa74d87d728c6df6df9cd (patch) | |
tree | a02ec0fa4eab777c5d103f55f80af9a83c4b43c8 /gn3/db | |
parent | 85564f9243b2d24eeaa1d574785c34ab1da65981 (diff) | |
download | genenetwork3-b24f7c6143dd1182819aa74d87d728c6df6df9cd.tar.gz |
Add functions that wrap around the common jsonld patterns.
* gn3/db/rdf.py: Import jsonld.
(query_frame_and_compact, query_and_compact, query_and_frame): New
functions.
Signed-off-by: Munyoki Kilyungi <me@bonfacemunyoki.com>
Diffstat (limited to 'gn3/db')
-rw-r--r-- | gn3/db/rdf.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py index 2140694..ae6b0f2 100644 --- a/gn3/db/rdf.py +++ b/gn3/db/rdf.py @@ -6,6 +6,7 @@ This module is a collection of functions that handle SPARQL queries. import json from SPARQLWrapper import SPARQLWrapper +from pyld import jsonld # type: ignore PREFIXES = { @@ -44,3 +45,27 @@ def sparql_construct_query(query: str, endpoint: str) -> dict: sparql.setQuery(query) results = sparql.queryAndConvert() return json.loads(results.serialize(format="json-ld")) # type: ignore + + +def query_frame_and_compact(query: str, context: dict, endpoint: str) -> dict: + """Frame and then compact the results given a context""" + results = sparql_construct_query(query, endpoint) + if not results: + return {} + return jsonld.compact(jsonld.frame(results, context), context) + + +def query_and_compact(query: str, context: dict, endpoint: str) -> dict: + """Compact the results given a context""" + results = sparql_construct_query(query, endpoint) + if not results: + return {} + return jsonld.compact(results, context) + + +def query_and_frame(query: str, context: dict, endpoint: str) -> dict: + """Frame the results given a context""" + results = sparql_construct_query(query, endpoint) + if not results: + return {} + return jsonld.frame(results, context) |