summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNicholas Car <nicholas.car@surroundaustralia.com>2021-06-18 20:02:29 +1000
committerNicholas Car <nicholas.car@surroundaustralia.com>2021-06-18 20:02:29 +1000
commita4727c1494a4cc93f8d78620db558aa3c23e83e6 (patch)
tree328d9b9f27cf292557adbe8b3a8a9b3cc0f83eb7 /examples
parent2bc50a48c1cb4f1d932ea3541dbd1aab6ee5b91b (diff)
downloadrdflib-a4727c1494a4cc93f8d78620db558aa3c23e83e6.tar.gz
ensure all examples work
Diffstat (limited to 'examples')
-rw-r--r--examples/slice.py10
-rw-r--r--examples/smushing.py2
-rw-r--r--examples/sparql_query_example.py4
-rw-r--r--examples/sparql_update_example.py14
-rw-r--r--examples/sparqlstore_example.py7
-rw-r--r--examples/swap_primer.py30
6 files changed, 35 insertions, 32 deletions
diff --git a/examples/slice.py b/examples/slice.py
index 33aacf9b..de3bb731 100644
--- a/examples/slice.py
+++ b/examples/slice.py
@@ -1,7 +1,7 @@
"""
RDFLib Graphs (and Resources) can be "sliced" with [] syntax
-This is a short-hand for iterating over triples
+This is a short-hand for iterating over triples.
Combined with SPARQL paths (see ``foafpaths.py``) - quite complex queries
can be realised.
@@ -15,13 +15,11 @@ from rdflib.namespace import FOAF
if __name__ == "__main__":
graph = Graph()
-
graph.load("foaf.n3", format="n3")
- for person in graph[: RDF.type : FOAF.Person]:
-
- friends = list(graph[person : FOAF.knows * "+" / FOAF.name])
+ for person in graph[: RDF.type: FOAF.Person]:
+ friends = list(graph[person: FOAF.knows * "+" / FOAF.name])
if friends:
- print("%s's circle of friends:" % graph.value(person, FOAF.name))
+ print(f"{graph.value(person, FOAF.name)}'s circle of friends:")
for name in friends:
print(name)
diff --git a/examples/smushing.py b/examples/smushing.py
index c1c3cb8b..8cdb13b4 100644
--- a/examples/smushing.py
+++ b/examples/smushing.py
@@ -42,4 +42,4 @@ if __name__ == "__main__":
o = newURI.get(o, o) # might be linked to another person
out.add((s, p, o))
- print(out.serialize(format="n3").decode("utf-8"))
+ print(out.serialize())
diff --git a/examples/sparql_query_example.py b/examples/sparql_query_example.py
index ce84b17d..99c75351 100644
--- a/examples/sparql_query_example.py
+++ b/examples/sparql_query_example.py
@@ -4,7 +4,7 @@ SPARQL Query using :meth:`rdflib.graph.Graph.query`
The method returns a :class:`~rdflib.query.Result`, iterating over
this yields :class:`~rdflib.query.ResultRow` objects
-The variable bindings can be access as attributes of the row objects
+The variable bindings can be accessed as attributes of the row objects
For variable names that are not valid python identifiers, dict access
(i.e. with ``row[var] / __getitem__``) is also possible.
@@ -18,7 +18,7 @@ if __name__ == "__main__":
g = rdflib.Graph()
g.load("foaf.n3", format="n3")
- # the QueryProcessor knows the FOAF prefix from the graph
+ # The QueryProcessor knows the FOAF prefix from the graph
# which in turn knows it from reading the N3 RDF file
for row in g.query("SELECT ?s WHERE { [] foaf:knows ?s .}"):
print(row.s)
diff --git a/examples/sparql_update_example.py b/examples/sparql_update_example.py
index fd7bce4e..aa591034 100644
--- a/examples/sparql_update_example.py
+++ b/examples/sparql_update_example.py
@@ -9,17 +9,19 @@ if __name__ == "__main__":
g = rdflib.Graph()
g.load("foaf.n3", format="n3")
- print("Initially there are {} triples in the graph".format(len(g)))
+ print(f"Initially there are {len(g)} triples in the graph")
g.update(
"""
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
- INSERT
- { ?s a dbpedia:Human . }
- WHERE
- { ?s a foaf:Person . }
+ INSERT {
+ ?s a dbpedia:Human .
+ }
+ WHERE {
+ ?s a foaf:Person .
+ }
"""
)
- print("After the UPDATE, there are {} triples in the graph".format(len(g)))
+ print(f"After the UPDATE, there are {len(g)} triples in the graph")
diff --git a/examples/sparqlstore_example.py b/examples/sparqlstore_example.py
index 1d2ed958..7d0ac681 100644
--- a/examples/sparqlstore_example.py
+++ b/examples/sparqlstore_example.py
@@ -20,6 +20,7 @@ if __name__ == "__main__":
int(pop), ",d"
).replace(",", ".")
)
+ print()
# EXAMPLE 2: using a SPARQLStore object directly
st = SPARQLStore(query_endpoint="http://dbpedia.org/sparql")
@@ -27,10 +28,12 @@ if __name__ == "__main__":
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")
+ "{0:,}".format(int(p), ",d")
)
+ print()
# EXAMPLE 3: doing RDFlib triple navigation using SPARQLStore as a Graph()
+ print("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
@@ -38,7 +41,7 @@ if __name__ == "__main__":
from rdflib.namespace import RDF, SKOS
for s in graph.subjects(predicate=RDF.type, object=SKOS.Concept):
count += 1
- print(s)
+ print(f"\t- {s}")
if count >= 3:
break
diff --git a/examples/swap_primer.py b/examples/swap_primer.py
index 35dc107c..dad76211 100644
--- a/examples/swap_primer.py
+++ b/examples/swap_primer.py
@@ -5,7 +5,7 @@ example stuff in the Primer on N3:
http://www.w3.org/2000/10/swap/Primer
"""
-from rdflib import ConjunctiveGraph, Namespace, Literal, URIRef
+from rdflib import ConjunctiveGraph, Namespace, Literal
from rdflib.namespace import OWL, DC
if __name__ == "__main__":
@@ -15,7 +15,7 @@ if __name__ == "__main__":
# Think of it as a blank piece of graph paper!
primer = ConjunctiveGraph()
- myNS = Namespace("#")
+ myNS = Namespace("https://example.com/")
primer.add((myNS.pat, myNS.knows, myNS.jo))
# or:
@@ -64,7 +64,7 @@ if __name__ == "__main__":
# Lets start with a verbatim string straight from the primer text:
mySource = """
- @prefix : <#> .
+ @prefix : <https://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@@ -75,25 +75,25 @@ if __name__ == "__main__":
<> dc:title
"Primer - Getting into the Semantic Web and RDF using N3".
- <#pat> <#knows> <#jo> .
- <#pat> <#age> 24 .
- <#al> is <#child> of <#pat> .
+ :pat :knows :jo .
+ :pat :age 24 .
+ :al :is_child_of :pat .
- <#pat> <#child> <#al>, <#chaz>, <#mo> ;
- <#age> 24 ;
- <#eyecolor> "blue" .
+ :pat :child :al, :chaz, :mo ;
+ :age 24 ;
+ :eyecolor "blue" .
- :Person a rdfs:Class.
+ :Person a rdfs:Class .
- :Pat a :Person.
+ :Pat a :Person .
:Woman a rdfs:Class; rdfs:subClassOf :Person .
- :sister a rdf:Property.
+ :sister a rdf:Property .
- :sister rdfs:domain :Person;
- rdfs:range :Woman.
+ :sister rdfs:domain :Person ;
+ rdfs:range :Woman .
:Woman = foo:FemaleAdult .
:Title a rdf:Property; = dc:title .
@@ -124,7 +124,7 @@ if __name__ == "__main__":
print()
print("Printing bigger example as N3:")
- print(primer.serialize(format="n3").decode("utf-8"))
+ print(primer.serialize(format="n3"))
# for more insight into things already done, lets see the namespaces