prepared_query
SPARQL Queries be prepared (i.e parsed and translated to SPARQL algebra)
by the prepareQuery method.
initNs can be used instead of PREFIX values.
When executing, variables can be bound with the
initBindings keyword parameter.
Attributes:
-
EXAMPLES_DIR– -
g– -
q– -
tim–
q
module-attribute
q = prepareQuery('SELECT ?name WHERE { ?person foaf:knows/foaf:name ?name . }', initNs={'foaf': FOAF})
rce: false show_docstring_attributes: false show_docstring_functions: false show_docstring_modules: false show_docstring_classes: false show_signature: false show_signature_annotations: false show_signature_type_parameters: false show_docstring_other_parameters: false show_docstring_parameters: false show_docstring_raises: false show_docstring_receives: false show_docstring_returns: false summary: false show_if_no_docstring: false
from pathlib import Path
import rdflib
from rdflib.namespace import FOAF
from rdflib.plugins.sparql import prepareQuery
EXAMPLES_DIR = Path(__file__).parent
if __name__ == "__main__":
q = prepareQuery(
"SELECT ?name WHERE { ?person foaf:knows/foaf:name ?name . }",
initNs={"foaf": FOAF},
)
g = rdflib.Graph()
g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}")
tim = rdflib.URIRef("http://www.w3.org/People/Berners-Lee/card#i")
for row in g.query(q, initBindings={"person": tim}):
# For select queries, the Result object is an iterable of ResultRow
# objects.
assert isinstance(row, rdflib.query.ResultRow)
print(row.name)