Package SPARQL :: Module Wrapper :: Class SPARQLWrapper
[hide private]
[frames] | no frames]

Class SPARQLWrapper

source code

Known Subclasses:
MyWrapper.MySPARQLWrapper, Results.SPARQLWrapper2

Wrapper around an online access to a SPARQL entry point on the Web.

Instance Methods [hide private]
 
__init__(self, baseURI, returnFormat='xml', defaultGraph=None)
Class encapsulating a full SPARQL call.
source code
 
setReturnFormat(self, format)
Set the return format.
source code
 
addDefaultGraph(self, uri)
Add a default graph URI
source code
 
addNamedGraph(self, uri)
Add a named graph URI
source code
 
addExtraURITag(self, key, value)
Some SPARQL endpoints require extra key value pairs for out-of-band settings.
source code
 
setQuery(self, query)
Set the SPARQL query text.
source code
string
getURI(self)
Return the URI as sent (or to be sent) to the SPARQL endpoint.
source code
 
query(self)
Execute the query.
source code
Python dictionary
convertJSON(self, queryResult)
Convert a JSON result into a Python dict.
source code
PyXlib DOM node
convertXML(self, queryResult)
Convert an XML result into a Python dom tree.
source code
RDFLib Graph
convertRDF(self, queryResult)
Convert an RDF/XML result into an RDFLib triple store.
source code
 
convert(self, queryResult)
Encode the return value depending on the return format:
source code
 
queryAndConvert(self)
Macro like method: issue a query and return the converted results.
source code
Method Details [hide private]

__init__(self, baseURI, returnFormat='xml', defaultGraph=None)
(Constructor)

source code 
Class encapsulating a full SPARQL call.
Parameters:
  • baseURI (string) - string of the SPARQL endpoint's URI
  • returnFormat (string) - the default is XML. Can be set to JSON or Turtle (but no local check is done, the parameter is simply sent to the endpoint. Eg, if the value is set to JSON and a construct query is issued, it is up to the endpoint to react or not, this wrapper does not check). Possible values: JSON, XML, TURTLE (constants in this module). The value can also be set via explicit call, see below.
  • defaultGraph (string) - URI for the default graph. Default is None, can be set via an explicit call, too

setReturnFormat(self, format)

source code 
Set the return format. If not an allowed value, the setting is ignored
Parameters:
  • format (string) - Possible values: are JSON, XML, TURTLE (constants in this module)

addDefaultGraph(self, uri)

source code 
Add a default graph URI
Parameters:
  • uri (string) - URI of the graph

addNamedGraph(self, uri)

source code 
Add a named graph URI
Parameters:
  • uri (string) - URI of the graph

addExtraURITag(self, key, value)

source code 
Some SPARQL endpoints require extra key value pairs for out-of-band settings. E.g., in virtuoso, one would add should-sponge=soft to the query to force virtuoso to retrieve graphs that are not stored in its local database These are added to the final query URI
Parameters:
  • key (string) - key of the query part
  • value (string) - value of the query part

setQuery(self, query)

source code 
Set the SPARQL query text. Note: no check is done on the query (syntax or otherwise) by this module!
Parameters:
  • query (string) - query text

getURI(self)

source code 
Return the URI as sent (or to be sent) to the SPARQL endpoint. The URI is constructed with the base URI given at initialization, plus all the other parameters set.
Returns: string
URI

query(self)

source code 

Execute the query. Exceptions can be raised if either the URI is wrong or the HTTP sends back an error. The usual urllib2 exceptions are raised, which cover possible SPARQL errors, too

The return is a file-like object with two additional methods: geturl() to return the URL of the resource retrieved and info() that returns the meta-information of the page, as a dictionary-like object (see the urllib2 standard library module of Python)

Note that in the case of errors (eg, raised by the sparql processor), the urllib2 raises exceptions. Refer to the Python documentation on those.
Returns:
query result

convertJSON(self, queryResult)

source code 
Convert a JSON result into a Python dict. This method can be overwritten in a subclass for a different conversion method.
Parameters:
  • queryResult - result of a (previous) query
Returns: Python dictionary
converted result

convertXML(self, queryResult)

source code 
Convert an XML result into a Python dom tree. This method can be overwritten in a subclass for a different conversion method.
Parameters:
  • queryResult - result of a previous query
Returns: PyXlib DOM node
converted result

convertRDF(self, queryResult)

source code 
Convert an RDF/XML result into an RDFLib triple store. This method can be overwritten in a subclass for a different conversion method.
Parameters:
  • queryResult - result of a previous query
Returns: RDFLib Graph
converted result

convert(self, queryResult)

source code 
Encode the return value depending on the return format:
  • in the case of XML, a minidom top level is returned
  • in the case of JSON, a simplejson conversion will return a dictionary
  • in the case of RDF/XML, the value is converted via RDFLib
in all other cases the input will simply be returned
Parameters:
  • queryResult - result of a previous query. In fact, it is the file-like object returned by the urllib2.urlopen function.
Returns:
the converted query result. See the conversion methods for more details.

queryAndConvert(self)

source code 
Macro like method: issue a query and return the converted results.
Returns:
the converted query result. See the conversion methods for more details.