Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast XML Datatypes in SPARQL query results to native Python types? #1178

Closed
dhimmel opened this issue Oct 5, 2020 · 1 comment
Closed

Cast XML Datatypes in SPARQL query results to native Python types? #1178

dhimmel opened this issue Oct 5, 2020 · 1 comment

Comments

@dhimmel
Copy link

dhimmel commented Oct 5, 2020

I've begun using rdflib to query OWL ontologies using SPARQL. Thanks for this Python tool!

Currently, I am using the API like:

results = graph.query(query) # graph is a rdflib.graph.Graph, and query is a string
result = results.bindings[0]

If we then look at result:

{
    rdflib.term.Variable('sub_efo_uri'): rdflib.term.URIRef('http://www.ebi.ac.uk/efo/EFO_0002508'), 
    rdflib.term.Variable('boolean_col'): rdflib.term.Literal('false', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#boolean')),
    rdflib.term.Variable('count'): rdflib.term.Literal('1', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer')),
}

Values are wrapped in rdflib.term.* objects. I noticed in these docs, a reference to the internal function rdflib.term._castLexicalToPython.

What is the recommended way to cast XML Datatypes from a SPARQL query result to native Python types? In my case, something that would produce output like:

{
    'sub_efo_uri': 'http://www.ebi.ac.uk/efo/EFO_0002508', 
    'boolean_col': False,
    'count': 1,
}

Is there a straightforward way to get SPARQL results with Python types this I am overlooking?

@dhimmel
Copy link
Author

dhimmel commented Oct 6, 2020

@matentzn provided the answer to my question in RDFLib/sparqlwrapper#125 (comment): the .toPython() method!

What is the recommended way to cast XML Datatypes from a SPARQL query result to native Python types? In my case, something that would produce output like:

The following extension of the above code gets the desired output:

{str(k): None if v is None else v.toPython() for k, v in result.items()}

There are two actionable changes that could be considered:

  1. expanding the documentation to mention .toPython(). Currently, it's used in an example (lit2006.toPython()), but much less obvious than the internal method rdflib.term._castLexicalToPython.

  2. adding a method/property to rdflib.plugins.sparql.processor.SPARQLResult to make accessing .toPython values easy. For example, something like SPARQLResult.python_bindings, which would be like bindings but using native Python types. I am guessing this is what most users want when they make a query.

@ghost ghost locked and limited conversation to collaborators Dec 26, 2021
@ghost ghost converted this issue into discussion #1599 Dec 26, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant