Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Virtuoso helper for SPARQLWrapper 1.6.0
from SPARQLWrapper import SPARQLWrapper
class VirtuosoWrapper(SPARQLWrapper):
def setParameter(self, name, value):
self.clearParameter(name)
return self.addParameter(name, value)
def setReturnFormat(self, output_format):
if output_format == 'jsonld':
self.returnFormat = 'jsonld'
else:
super().setReturnFormat(output_format)
def _virtuoso_output_format(self):
if self.queryType in ['SELECT', 'ASK', 'INSERT', 'DELETE', 'MODIFY']:
if self.returnFormat == 'xml':
output_format = b'application/sparql-results+xml'
elif self.returnFormat == 'json':
output_format = b'application/sparql-results+json'
else:
output_format = b'application/sparql-results+json'
else:
if self.returnFormat == 'n3' or self.returnFormat == 'turtle':
output_format = b'text/turtle'
elif self.returnFormat == 'xml':
output_format = b'application/xml+rdf'
elif self.returnFormat == 'jsonld':
output_format = b'application/x-json+ld'
else:
output_format = b'application/x-json+ld'
return output_format
def _getRequestParameters(self):
parameters = super()._getRequestParameters()
for param in ["format", "output", "results"]:
parameters.pop(param, None)
parameters[b"format"] = self._virtuoso_output_format()
return parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment