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

Class QueryResult

source code

Wrapper around an a query result. Users should not create instances of this class, it is generated by a SPARQLWrapper.query call. The results can be converted to various formats, or used directly.

If used directly: the class gives access to the direct http request results self.response: it 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 HTTP result as a dictionary-like object (see the urllib2 standard library module of Python).

For convenience, these methods are also available on the instance. The __iter__ and next methods are also implemented (by mapping them to self.response). This means that the common idiom:

for l in obj : do_something_with_line(l)

would work, too.

Instance Methods [hide private]
 
__init__(self, response) source code
string
geturl(self)
Return the URI of the original call.
source code
dictionary
info(self)
Return the meta-information of the HTTP result.
source code
 
__iter__(self)
Return an iterator object.
source code
 
next(self)
Method for the standard iterator.
source code
Python dictionary
_convertJSON(self)
Convert a JSON result into a Python dict.
source code
PyXlib DOM node
_convertXML(self)
Convert an XML result into a Python dom tree.
source code
RDFLib Graph
_convertRDF(self)
Convert an RDF/XML result into an RDFLib triple store.
source code
string
_convertN3(self)
Convert an RDF Turtle/N3 result into a string.
source code
 
convert(self)
Encode the return value depending on the return format:
source code
Instance Variables [hide private]
  response
the direct HTTP response; a file-like object, as return by the urllib2.urlopen library call.
Method Details [hide private]

__init__(self, response)
(Constructor)

source code 
Parameters:

geturl(self)

source code 

Return the URI of the original call.

Returns: string
URI

info(self)

source code 

Return the meta-information of the HTTP result.

Returns: dictionary
meta information

__iter__(self)

source code 

Return an iterator object. This method is expected for the inclusion of the object in a standard for loop.

_convertJSON(self)

source code 

Convert a JSON result into a Python dict. This method can be overwritten in a subclass for a different conversion method.

Returns: Python dictionary
converted result

_convertXML(self)

source code 

Convert an XML result into a Python dom tree. This method can be overwritten in a subclass for a different conversion method.

Returns: PyXlib DOM node
converted result

_convertRDF(self)

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.

Returns: RDFLib Graph
converted result

_convertN3(self)

source code 

Convert an RDF Turtle/N3 result into a string. This method can be overwritten in a subclass for a different conversion method.

Returns: string
converted result

convert(self)

source code 

Encode the return value depending on the return format:

  • in the case of XML, a DOM top element 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 into a Graph instance.

In all other cases the input simply returned.

Returns:
the converted query result. See the conversion methods for more details.