summaryrefslogtreecommitdiff
path: root/examples/slice.py
diff options
context:
space:
mode:
authorGunnar Aastrand Grimnes <gromgull@gmail.com>2013-05-02 11:23:19 +0200
committerGunnar Aastrand Grimnes <gromgull@gmail.com>2013-05-02 11:23:19 +0200
commitbdbc2a580dd1c6064b665d6bbbc3bfff6f02c783 (patch)
treea071cfabe4a9ef3ff120f742c3bcf3f96817e510 /examples/slice.py
parent557b1012bed875434001ca02be232159cd3b9d21 (diff)
downloadrdflib-bdbc2a580dd1c6064b665d6bbbc3bfff6f02c783.tar.gz
examples updates, some slice fixes
Diffstat (limited to 'examples/slice.py')
-rw-r--r--examples/slice.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/examples/slice.py b/examples/slice.py
new file mode 100644
index 00000000..a06b3ee8
--- /dev/null
+++ b/examples/slice.py
@@ -0,0 +1,26 @@
+"""
+
+RDFLib Graphs (and Resources) can be "sliced" with [] syntax
+
+This is a short-hand for iterating over triples
+
+Combined with SPARQL paths (see foafpaths.py) - quite complex queries
+can be realised
+
+"""
+
+from rdflib import Graph, RDF
+from rdflib.namespace import FOAF
+
+graph = Graph()
+
+graph.load("foaf.rdf")
+
+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)
+ for _,_,name in friends:
+ print name
+