SPARQLWrapper.SmartWrapper module

class SPARQLWrapper.SmartWrapper.Bindings(retval)[source]

Bases: object

Class encapsulating one query result, based on the JSON return format. It decodes the return values to make it a bit more usable for a standard usage. The class consumes the return value and instantiates a number of attributes that can be consulted directly. See the list of variables.

The Serializing SPARQL Query Results in JSON explains the details of the JSON return structures. Very succinctly: the return data has “bindings”, which means a list of dictionaries. Each dictionary is a possible binding of the SELECT variables to Value instances. This structure is made a bit more usable by this class.

Variables:
  • fullResult (dict) – The original dictionary of the results, stored for an easier reference.
  • head (dict) – Header part of the return, see the JSON return format document for details.
  • variables (list) – List of unbounds (variables) of the original query. It is a list of strings. None in the case of an ASK query.
  • bindings (list) – The final bindings: list of dictionaries, mapping variables to Value instances. If unbound, then no value is set in the dictionary; that can be easily checked with var in res.bindings[..], for example.
  • askResult (bool) – by default, set to False; in case of an ASK query, the result of the query.
__init__(retval)[source]
Parameters:retval (QueryResult) – the query result.
convert()[source]

This is just a convenience method, returns self.

Although SPARQLWrapper2.Bindings is not a subclass of SPARQLWrapper.QueryResult, it is returned as a result by SPARQLWrapper2.query(), just like QueryResult is returned by SPARQLWrapper.query(). Consequently, having an empty convert() method to imitate QueryResult's convert() method may avoid unnecessary problems.

getValues(key)[source]

A shorthand for the retrieval of all bindings for a single key. It is equivalent to [b[key] for b in self[key]]

Parameters:key (string) – possible variable name.
Returns:list of Value instances.
Return type:list
class SPARQLWrapper.SmartWrapper.SPARQLWrapper2(baseURI, defaultGraph=None)[source]

Bases: SPARQLWrapper.Wrapper.SPARQLWrapper

Subclass of SPARQLWrapper that works with a JSON SELECT return result only. The query result is automatically set to a Bindings instance. Makes the average query processing a bit simpler…

__init__(baseURI, defaultGraph=None)[source]

Class encapsulating a full SPARQL call. In contrast to the SPARQLWrapper superclass, the return format cannot be set (it is defaulted to JSON).

Parameters:
  • baseURI (string) – string of the SPARQL endpoint’s URI.
  • defaultGraph (string) – URI for the default graph. Default is None, can be set via an explicit call, too.
query()[source]

Execute the query and do an automatic conversion.

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.

If the query type is not SELECT, the method falls back to the corresponding method in the superclass.

Returns:query result
Return type:Bindings instance
queryAndConvert()[source]

This is here to override the inherited method; it is equivalent to query.

If the query type is not SELECT, the method falls back to the corresponding method in the superclass.

Returns:the converted query result.
setReturnFormat(format)[source]

Set the return format (overriding the inherited method).

Warning

This method does nothing; this class instance should work with JSON only. The method is defined just to avoid possible errors by erroneously setting the return format. When using this class, the user can safely ignore this call.

Parameters:format (string) – return format
class SPARQLWrapper.SmartWrapper.Value(variable, binding)[source]

Bases: object

Class encapsulating a single binding for a variable.

Variables:
  • variable (string) – The original variable, stored for an easier reference.
  • value (string) – Value of the binding.
  • type (string) – Type of the binding. One of Value.URI, Value.Literal, Value.TypedLiteral, or Value.BNODE.
  • lang (string) – Language tag of the binding, or None if not set.
  • datatype (string) – Datatype of the binding, or None if not set. It is an URI.
BNODE = 'bnode'

the string denoting a blank node variable.

Literal = 'literal'

the string denoting a Literal variable.

TypedLiteral = 'typed-literal'

the string denoting a typed literal variable.

URI = 'uri'

the string denoting a URI variable.

__init__(variable, binding)[source]
Parameters:
  • variable (string) – the variable for that binding. Stored for an easier reference.
  • binding (dict) – the binding dictionary part of the return result for a specific binding.