Skip to content

Instantly share code, notes, and snippets.

@indeyets
Created May 9, 2014 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indeyets/f806bf36ce6df7e358e8 to your computer and use it in GitHub Desktop.
Save indeyets/f806bf36ce6df7e358e8 to your computer and use it in GitHub Desktop.
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