diff options
author | Nicholas Car <nicholas.car@surroundaustralia.com> | 2021-06-28 00:06:31 +1000 |
---|---|---|
committer | Nicholas Car <nicholas.car@surroundaustralia.com> | 2021-06-28 00:06:31 +1000 |
commit | 2949e0b1f54ae801202ee522a23112f026713ddd (patch) | |
tree | bec47f0992bd4a3e0baed541f4af4dd6c90017d3 /examples | |
parent | 79a8372c3bfd714a9f4e378c692b656862f8e5c6 (diff) | |
parent | 69320abd14c14bd19a9bea16c8a49dc18d13159c (diff) | |
download | rdflib-2949e0b1f54ae801202ee522a23112f026713ddd.tar.gz |
Merge branch 'master' of https://github.com/RDFLib/rdflib into berkeleydb
# Conflicts:
# docs/intro_to_sparql.rst
# examples/conjunctive_graphs.py
# examples/simple_example.py
# examples/sleepycat_example.py
# tox.ini
Diffstat (limited to 'examples')
-rw-r--r-- | examples/simple_example.py | 59 | ||||
-rw-r--r-- | examples/smushing.py | 2 | ||||
-rw-r--r-- | examples/sparqlstore_example.py | 4 | ||||
-rw-r--r-- | examples/swap_primer.py | 2 |
4 files changed, 63 insertions, 4 deletions
diff --git a/examples/simple_example.py b/examples/simple_example.py new file mode 100644 index 00000000..49f08408 --- /dev/null +++ b/examples/simple_example.py @@ -0,0 +1,59 @@ +from rdflib import Graph, Literal, BNode, RDF +from rdflib.namespace import FOAF, DC + +if __name__ == "__main__": + + store = Graph() + + # Bind a few prefix, namespace pairs for pretty output + store.bind("dc", DC) + store.bind("foaf", FOAF) + + # Create an identifier to use as the subject for Donna. + donna = BNode() + + # Add triples using store's add method. + store.add((donna, RDF.type, FOAF.Person)) + store.add((donna, FOAF.nick, Literal("donna", lang="foo"))) + store.add((donna, FOAF.name, Literal("Donna Fales"))) + + # Iterate over triples in store and print them out. + print("--- printing raw triples ---") + for s, p, o in store: + print(s, p, o) + + # For each foaf:Person in the store print out its mbox property. + print() + print("--- printing mboxes ---") + for person in store.subjects(RDF.type, FOAF["Person"]): + for mbox in store.objects(person, FOAF["mbox"]): + print(mbox) + + print("--- saving RDF to a file (donna_foaf.rdf) ---") + # Serialize the store as RDF/XML to the file donna_foaf.rdf. + store.serialize("donna_foaf.rdf", format="pretty-xml", max_depth=3) + + # Let's show off the serializers + print() + print("RDF Serializations:") + + # Serialize as Turtle (default) + print("--- start: turtle ---") + print(store.serialize()) + print("--- end: turtle ---\n") + + # Serialize as XML + print("--- start: rdf-xml ---") + print(store.serialize(format="pretty-xml")) + print("--- end: rdf-xml ---\n") + + # Serialize as NTriples + print("--- start: ntriples ---") + print(store.serialize(format="nt")) + print("--- end: ntriples ---\n") + + # Serialize as JSON-LD + # only if you have the JSON-LD plugin installed! + print("--- start: JSON-LD ---") + print(store.serialize(format="json-ld")) + print("--- end: JSON-LD ---\n") diff --git a/examples/smushing.py b/examples/smushing.py index c1c3cb8b..e9ad8f6f 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(format="n3")) diff --git a/examples/sparqlstore_example.py b/examples/sparqlstore_example.py index 1d2ed958..986f883c 100644 --- a/examples/sparqlstore_example.py +++ b/examples/sparqlstore_example.py @@ -17,7 +17,7 @@ if __name__ == "__main__": print( "According to DBPedia, Berlin has a population of {0:,}".format( - int(pop), ",d" + int(pop) ).replace(",", ".") ) @@ -27,7 +27,7 @@ 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(pop)) ) # EXAMPLE 3: doing RDFlib triple navigation using SPARQLStore as a Graph() diff --git a/examples/swap_primer.py b/examples/swap_primer.py index 35dc107c..45327c5f 100644 --- a/examples/swap_primer.py +++ b/examples/swap_primer.py @@ -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 |