diff options
author | Nicholas Car <nicholas.car@surroundaustralia.com> | 2020-05-12 08:59:29 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-12 08:59:29 +1000 |
commit | 6197531775801c61a620c8094327d145516a5ed7 (patch) | |
tree | fe5d980f65f59952e01b2dacc45aff6ce43ca624 | |
parent | a3245fb1fac921a5d8e07bce80914c42f5399b32 (diff) | |
parent | e2d6dae0020000d26e7e31c9161730c04505ce6e (diff) | |
download | rdflib-6197531775801c61a620c8094327d145516a5ed7.tar.gz |
Merge pull request #1041 from dwinston/add-service-clause-to-docs
Add SERVICE clause to documentation
-rw-r--r-- | docs/intro_to_sparql.rst | 29 | ||||
-rw-r--r-- | rdflib/plugins/sparql/evaluate.py | 2 | ||||
-rw-r--r-- | test/test_sparql_service.py | 15 |
3 files changed, 45 insertions, 1 deletions
diff --git a/docs/intro_to_sparql.rst b/docs/intro_to_sparql.rst index 5ab34287..97c7f281 100644 --- a/docs/intro_to_sparql.rst +++ b/docs/intro_to_sparql.rst @@ -71,6 +71,35 @@ Variables can also be pre-bound, using ``initBindings`` kwarg can be used to pass in a ``dict`` of initial bindings, this is particularly useful for prepared queries, as described below. +Query a Remote Service +^^^^^^^^^^^^^^^^^^^^^^ + +The SERVICE keyword of SPARQL 1.1 can send a query to a remote SPARQL endpoint. + +.. code-block:: python + + import rdflib + + g = rdflib.Graph() + qres = g.query(''' + SELECT ?s + WHERE { + SERVICE <http://dbpedia.org/sparql> { + ?s <http://purl.org/linguistics/gold/hypernym> <http://dbpedia.org/resource/Leveller> . + } + } LIMIT 3''') + for row in qres: + print(row.s) + +This example sends a query to `DBPedia +<https://dbpedia.org/>`_'s SPARQL endpoint service so that it can run the query and then send back the result: + +.. code-block:: text + + http://dbpedia.org/resource/Elizabeth_Lilburne + http://dbpedia.org/resource/Thomas_Prince_(Leveller) + http://dbpedia.org/resource/John_Lilburne + Prepared Queries ^^^^^^^^^^^^^^^^ diff --git a/rdflib/plugins/sparql/evaluate.py b/rdflib/plugins/sparql/evaluate.py index 0973b22b..cc6d35f9 100644 --- a/rdflib/plugins/sparql/evaluate.py +++ b/rdflib/plugins/sparql/evaluate.py @@ -277,7 +277,7 @@ def evalPart(ctx, part): def evalServiceQuery(ctx, part): res = {} match = re.match('^service <(.*)>[ \n]*{(.*)}[ \n]*$', - part.get('service_string', ''), re.DOTALL) + part.get('service_string', ''), re.DOTALL | re.I) if match: service_url = match.group(1) diff --git a/test/test_sparql_service.py b/test/test_sparql_service.py index 45cb84a0..19f713c3 100644 --- a/test/test_sparql_service.py +++ b/test/test_sparql_service.py @@ -99,6 +99,7 @@ def test_service_with_implicit_select_and_prefix(): for r in results: assert len(r) == 3 + def test_service_with_implicit_select_and_base(): g = Graph() q = '''base <http://example.org/> @@ -116,6 +117,20 @@ def test_service_with_implicit_select_and_base(): assert len(r) == 3 +def test_service_with_implicit_select_and_allcaps(): + g = Graph() + q = '''SELECT ?s + WHERE + { + SERVICE <http://dbpedia.org/sparql> + { + ?s <http://purl.org/linguistics/gold/hypernym> <http://dbpedia.org/resource/Leveller> . + } + } LIMIT 3''' + results = g.query(q) + assert len(results) == 3 + + #def test_with_fixture(httpserver): # httpserver.expect_request("/sparql/?query=SELECT * WHERE ?s ?p ?o").respond_with_json({"vars": ["s","p","o"], "bindings":[]}) # test_server = httpserver.url_for('/sparql') |