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

Class SPARQLWrapper

source code

object --+
         |
        SPARQLWrapper
Known Subclasses:

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

The same class instance can be reused for subsequent queries. The values of the base Graph URI, return formats, etc, are retained from one query to the next (in other words, only the query string changes). The instance can also be reset to its initial values using the resetQuery method.

Instance Methods [hide private]
 
__init__(self, endpoint, updateEndpoint=None, returnFormat='xml', defaultGraph=None, agent='sparqlwrapper 1.6.2 (rdflib.github.io/sparqlwrapper)')
Class encapsulating a full SPARQL call.
source code
 
_createRequest(self)
Internal method to create request according a HTTP method.
source code
 
_getAcceptHeader(self) source code
 
_getRequestParameters(self) source code
string
_parseQueryType(self, query)
Parse the SPARQL query and return its type (ie, SELECT, ASK, etc).
source code
 
_query(self)
Internal method to execute the query.
source code
bool
addCustomParameter(*args, **kwargs)
Method is kept for backwards compatibility.
source code
 
addDefaultGraph(*args, **kwargs)
Add a default graph URI.
source code
 
addExtraURITag(*args, **kwargs)
Some SPARQL endpoints require extra key value pairs.
source code
 
addNamedGraph(*args, **kwargs)
Add a named graph URI.
source code
bool
addParameter(self, name, value)
Some SPARQL endpoints allow extra key value pairs.
source code
bool
clearParameter(self, name)
Clear the values ofd a concrete parameter.
source code
 
isSparqlQueryRequest(self)
Returns TRUE if SPARQLWrapper is configured for executing SPARQL Query request
source code
 
isSparqlUpdateRequest(self)
Returns TRUE if SPARQLWrapper is configured for executing SPARQL Update request
source code
QueryResult instance
query(self)
Execute the query.
source code
 
queryAndConvert(self)
Macro like method: issue a query and return the converted results.
source code
 
resetQuery(self)
Reset the query, ie, return format, query, default or named graph settings, etc, are reset to their default values.
source code
 
setCredentials(self, user, passwd)
Set the credentials for querying the current endpoint
source code
 
setMethod(self, method)
Set the invocation method.
source code
 
setQuery(self, query)
Set the SPARQL query text.
source code
 
setRequestMethod(self, method)
Set the internal method to use to perform the request for query or update operations, either URL-encoded (SPARQLWrapper.URLENCODED) or POST directly (SPARQLWrapper.POSTDIRECTLY).
source code
 
setReturnFormat(self, format)
Set the return format.
source code
 
setTimeout(self, timeout)
Set the timeout (in seconds) to use for querying the endpoint.
source code
 
setUpdateMethod(*args, **kwargs) source code
 
setUseKeepAlive(self)
Make urllib2 use keep-alive.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Variables [hide private]
compiled regular expression (see the re module of Python) pattern = re.compile(r'(?ix)((?P<base>(\s*BASE\s*<.*?>)\s*)|(?...
regular expression used to determine whether a query is of type CONSTRUCT, SELECT, ASK, or DESCRIBE.
Instance Variables [hide private]
  baseURI
the URI of the SPARQL service
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, endpoint, updateEndpoint=None, returnFormat='xml', defaultGraph=None, agent='sparqlwrapper 1.6.2 (rdflib.github.io/sparqlwrapper)')
(Constructor)

source code 

Class encapsulating a full SPARQL call.

Parameters:
  • endpoint (string) - string of the SPARQL endpoint's URI
  • updateEndpoint (string) - string of the SPARQL endpoint's URI for update operations (if it's a different one)
  • returnFormat (string) - Default: XML. Can be set to JSON or Turtle/N3

    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, N3 (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, the value can be set either via an explicit call or as part of the query string.
Overrides: object.__init__

_createRequest(self)

source code 

Internal method to create request according a HTTP method. Returns a urllib2.Request object of the urllib2 Python library

Returns:
request

_parseQueryType(self, query)

source code 

Parse the SPARQL query and return its type (ie, SELECT, ASK, etc).

Note that the method returns SELECT if nothing is specified. This is just to get all other methods running; in fact, this means that the query is erronous, because the query must be, according to the SPARQL specification, one of Select, Ask, Describe, or Construct. The SPARQL endpoint should raise an exception (via urllib) for such syntax error.

Parameters:
  • query (string) - query text
Returns: string

_query(self)

source code 

Internal method to execute the query. Returns the output of the urllib2.urlopen method of the standard Python library

Returns:
tuples with the raw request plus the expected format

addCustomParameter(*args, **kwargs)

source code 

Method is kept for backwards compatibility. Historically, it "replaces" parameters instead of adding

Parameters:
  • name (string) - name
  • value (string) - value
Returns: bool
Decorators:
  • @deprecated

Deprecated: use addParameter(name, value) instead of this method

addDefaultGraph(*args, **kwargs)

source code 

Add a default graph URI.

Parameters:
  • uri (string) - URI of the graph
Decorators:
  • @deprecated

Deprecated: use addParameter("default-graph-uri", uri) instead of this method

addExtraURITag(*args, **kwargs)

source code 

Some SPARQL endpoints require extra key value pairs. E.g., in virtuoso, one would add should-sponge=soft to the query forcing virtuoso to retrieve graphs that are not stored in its local database.

Parameters:
  • key (string) - key of the query part
  • value (string) - value of the query part
Decorators:
  • @deprecated

Deprecated: use addParameter(key, value) instead of this method

addNamedGraph(*args, **kwargs)

source code 

Add a named graph URI.

Parameters:
  • uri (string) - URI of the graph
Decorators:
  • @deprecated

Deprecated: use addParameter("named-graph-uri", uri) instead of this method

addParameter(self, name, value)

source code 

Some SPARQL endpoints allow extra key value pairs. E.g., in virtuoso, one would add should-sponge=soft to the query forcing virtuoso to retrieve graphs that are not stored in its local database.

Parameters:
  • name (string) - name
  • value (string) - value
Returns: bool

clearParameter(self, name)

source code 

Clear the values ofd a concrete parameter.

Parameters:
  • name (string) - name
Returns: bool

isSparqlQueryRequest(self)

source code 

Returns TRUE if SPARQLWrapper is configured for executing SPARQL Query request

Returns:
bool

isSparqlUpdateRequest(self)

source code 

Returns TRUE if SPARQLWrapper is configured for executing SPARQL Update request

Returns:
bool

query(self)

source code 

Execute the query. Exceptions can be raised if either the URI is wrong or the HTTP sends back an error (this is also the case when the query is syntactically incorrect, leading to an HTTP error sent back by the SPARQL endpoint). The usual urllib2 exceptions are raised, which therefore cover possible SPARQL errors, too.

Note that some combinations of return formats and query types may not make sense. For example, a SELECT query with Turtle response is meaningless (the output of a SELECT is not a Graph), or a CONSTRUCT query with JSON output may be a problem because, at the moment, there is no accepted JSON serialization of RDF (let alone one implemented by SPARQL endpoints). In such cases the returned media type of the result is unpredictable and may differ from one SPARQL endpoint implementation to the other. (Endpoints usually fall back to one of the "meaningful" formats, but it is up to the specific implementation to choose which one that is.)

Returns: QueryResult instance
query result

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.

setCredentials(self, user, passwd)

source code 

Set the credentials for querying the current endpoint

Parameters:
  • user (string) - username
  • passwd (string) - password

setMethod(self, method)

source code 

Set the invocation method. By default, this is GET, but can be set to POST.

Parameters:
  • method - should be either GET or POST. Other cases are ignored.

setQuery(self, query)

source code 

Set the SPARQL query text. Note: no check is done on the validity of the query (syntax or otherwise) by this module, except for testing the query type (SELECT, ASK, etc). Syntax and validity checking is done by the SPARQL service itself.

Parameters:
  • query (string) - query text

Bug: #2320024

setRequestMethod(self, method)

source code 

Set the internal method to use to perform the request for query or update operations, either URL-encoded (SPARQLWrapper.URLENCODED) or POST directly (SPARQLWrapper.POSTDIRECTLY). Further details at http://www.w3.org/TR/sparql11-protocol/#query-operation and http://www.w3.org/TR/sparql11-protocol/#update-operation.

Parameters:
  • method (str) - method

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, N3, RDF (constants in this module). All other cases are ignored.

setTimeout(self, timeout)

source code 

Set the timeout (in seconds) to use for querying the endpoint.

Parameters:
  • timeout (int) - Timeout in seconds.

setUpdateMethod(*args, **kwargs)

source code 
Decorators:
  • @deprecated

setUseKeepAlive(self)

source code 

Make urllib2 use keep-alive.

Raises:
  • ImportError - when could not be imported urlgrabber.keepalive.HTTPHandler

Class Variable Details [hide private]

pattern

regular expression used to determine whether a query is of type CONSTRUCT, SELECT, ASK, or DESCRIBE.
Type:
compiled regular expression (see the re module of Python)
Value:
re.compile(r'(?ix)((?P<base>(\s*BASE\s*<.*?>)\s*)|(?P<prefixes>(\s*PRE\
FIX\s+.+:\s*<.*?>)\s*))*(?P<queryType>(CONSTRUCT|SELECT|ASK|DESCRIBE|I\
NSERT|DELETE|CREATE|CLEAR|DROP|LOAD|COPY|MOVE|ADD))')