summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2020-10-03 21:13:06 +1000
committerNicholas Car <nicholas.car@surroundaustralia.com>2020-10-03 21:13:06 +1000
commit7cb6bb4c05e110e35e930b03e61b3cf75e7b2595 (patch)
tree037e78cf5a1eb6c33e60a3283911e24208ceb49b /examples
parenta30fb38b6c81e4586ed059616c8e539ec9dce06b (diff)
downloadrdflib-7cb6bb4c05e110e35e930b03e61b3cf75e7b2595.tar.gz
replace requests with urllib for SPARQLStore/SPARQLUpdateStore, update relevant e.g.s
Diffstat (limited to 'examples')
-rw-r--r--examples/sparqlstore_example.py40
1 files changed, 27 insertions, 13 deletions
diff --git a/examples/sparqlstore_example.py b/examples/sparqlstore_example.py
index 936f6540..1d2ed958 100644
--- a/examples/sparqlstore_example.py
+++ b/examples/sparqlstore_example.py
@@ -1,8 +1,7 @@
"""
-A simple example showing how to use the SPARQLStore
+Simple examples showing how to use the SPARQLStore
"""
-import locale
from rdflib import Graph, URIRef, Namespace
from rdflib.plugins.stores.sparqlstore import SPARQLStore
@@ -10,7 +9,7 @@ if __name__ == "__main__":
dbo = Namespace("http://dbpedia.org/ontology/")
- # using a Graph with the Store type string set to "SPARQLStore"
+ # EXAMPLE 1: using a Graph with the Store type string set to "SPARQLStore"
graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
graph.open("http://dbpedia.org/sparql")
@@ -22,13 +21,28 @@ if __name__ == "__main__":
).replace(",", ".")
)
- # using a SPARQLStore object directly
- s = SPARQLStore(endpoint="http://dbpedia.org/sparql")
- s.open(None)
- pop = graph.value(
- URIRef("http://dbpedia.org/resource/Brisbane"), dbo.populationTotal
- )
- print(
- "According to DBPedia, Brisbane has a population of "
- "{0:,}".format(int(pop), ",d")
- )
+ # EXAMPLE 2: using a SPARQLStore object directly
+ st = SPARQLStore(query_endpoint="http://dbpedia.org/sparql")
+
+ for p in st.objects(URIRef("http://dbpedia.org/resource/Brisbane"), dbo.populationTotal):
+ print(
+ "According to DBPedia, Brisbane has a population of "
+ "{0:,}".format(int(pop), ",d")
+ )
+
+ # EXAMPLE 3: doing RDFlib triple navigation using SPARQLStore as a Graph()
+ graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
+ graph.open("http://dbpedia.org/sparql")
+ # we are asking DBPedia for 3 skos:Concept instances
+ count = 0
+ from rdflib.namespace import RDF, SKOS
+ for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept):
+ count += 1
+ print(s)
+ if count >= 3:
+ break
+
+ # EXAMPLE 4: using a SPARQL endpoint that requires Basic HTTP authentication
+ # NOTE: this example won't run since the endpoint isn't live (or real)
+ s = SPARQLStore(query_endpoint="http://fake-sparql-endpoint.com/repository/x", auth=("my_username", "my_password"))
+ # do normal Graph things