summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorGunnar Aastrand Grimnes <gromgull@gmail.com>2013-03-09 08:06:45 +0100
committerGunnar Aastrand Grimnes <gromgull@gmail.com>2013-03-09 08:06:45 +0100
commit7ba4a4e40377fabc08db3833ad588ca484ba1535 (patch)
treecdf1018d3d6b5110f209c172217533780c1ff4ad /examples
parent89359325146cf4e80f812198a17333b7aca30626 (diff)
downloadrdflib-7ba4a4e40377fabc08db3833ad588ca484ba1535.tar.gz
cleaned up examples
Diffstat (limited to 'examples')
-rw-r--r--examples/custom_datatype.py39
-rw-r--r--examples/example.py58
-rw-r--r--examples/simple_example.py33
-rw-r--r--examples/sparql_query_example.py6
-rw-r--r--examples/swap_primer.py9
-rw-r--r--examples/update_namespace.py38
6 files changed, 63 insertions, 120 deletions
diff --git a/examples/custom_datatype.py b/examples/custom_datatype.py
new file mode 100644
index 00000000..cebb8ccc
--- /dev/null
+++ b/examples/custom_datatype.py
@@ -0,0 +1,39 @@
+"""
+
+rdflib.term.bind lets you register new mappings between literal
+datatypes and python objects
+
+"""
+
+
+from rdflib import Graph, Literal, Namespace, XSD
+from rdflib.term import bind
+
+# complex numbers are not registered by default
+# on custom constructor/serializer needed since
+# complex('(2+3j)') works fine
+bind(XSD.complexNumber, complex)
+
+ns=Namespace("urn:my:namespace:")
+
+c=complex(2,3)
+
+l=Literal(c)
+
+g=Graph()
+g.add((ns.mysubject, ns.myprop, l))
+
+n3=g.serialize(format='n3')
+
+# round-trip through n3
+
+g2=Graph()
+g2.parse(data=n3, format='n3')
+
+l2=list(g2)[0][2]
+
+print l2
+
+print l2.value == c # back to a python complex object
+
+
diff --git a/examples/example.py b/examples/example.py
deleted file mode 100644
index c1e808fc..00000000
--- a/examples/example.py
+++ /dev/null
@@ -1,58 +0,0 @@
-import logging
-
-# Configure how we want rdflib logger to log messages
-_logger = logging.getLogger("rdflib")
-_logger.setLevel(logging.DEBUG)
-_hdlr = logging.StreamHandler()
-_hdlr.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s'))
-_logger.addHandler(_hdlr)
-
-from rdflib.graph import Graph
-from rdflib.term import URIRef, Literal, BNode
-from rdflib.namespace import Namespace, RDF
-
-store = Graph()
-
-# Bind a few prefix, namespace pairs.
-store.bind("dc", "http://http://purl.org/dc/elements/1.1/")
-store.bind("foaf", "http://xmlns.com/foaf/0.1/")
-
-# Create a namespace object for the Friend of a friend namespace.
-FOAF = Namespace("http://xmlns.com/foaf/0.1/")
-
-# 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 "--- printing mboxes ---"
-for person in store.subjects(RDF.type, FOAF["Person"]):
- for mbox in store.objects(person, FOAF["mbox"]):
- print mbox
-
-# Serialize the store as RDF/XML to the file foaf.rdf.
-store.serialize("foaf.rdf", format="pretty-xml", max_depth=3)
-
-# Let's show off the serializers
-
-print "RDF Serializations:"
-
-# 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"
-
diff --git a/examples/simple_example.py b/examples/simple_example.py
index 06b0a901..1e6c0f67 100644
--- a/examples/simple_example.py
+++ b/examples/simple_example.py
@@ -7,9 +7,7 @@ _hdlr = logging.StreamHandler()
_hdlr.setFormatter(logging.Formatter('%(name)s %(levelname)s: %(message)s'))
_logger.addHandler(_hdlr)
-from rdflib.graph import Graph
-from rdflib import URIRef, Literal, BNode, Namespace
-from rdflib import RDF
+from rdflib import Graph, URIRef, Literal, BNode, Namespace, RDF
store = Graph()
@@ -29,29 +27,34 @@ 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 ---")
+print "--- printing raw triples ---"
for s, p, o in store:
- print((s, p, o))
+ print s, p, o
# For each foaf:Person in the store print out its mbox property.
-print("--- printing mboxes ---")
+print "--- printing mboxes ---"
for person in store.subjects(RDF.type, FOAF["Person"]):
for mbox in store.objects(person, FOAF["mbox"]):
- print(mbox)
+ print mbox
# Serialize the store as RDF/XML to the file foaf.rdf.
-# store.serialize("foaf.rdf", format="pretty-xml", max_depth=3)
+store.serialize("foaf.rdf", format="pretty-xml", max_depth=3)
# Let's show off the serializers
-print("RDF Serializations:")
+print "RDF Serializations:"
# Serialize as XML
-print("--- start: rdf-xml ---")
-print(store.serialize(format="pretty-xml"))
-print("--- end: rdf-xml ---\n")
+print "--- start: rdf-xml ---"
+print store.serialize(format="pretty-xml")
+print "--- end: rdf-xml ---\n"
+
+# Serialize as Turtle
+print "--- start: turtle ---"
+print store.serialize(format="turtle")
+print "--- end: turtle ---\n"
# Serialize as NTriples
-print("--- start: ntriples ---")
-print(store.serialize(format="nt"))
-print("--- end: ntriples ---\n")
+print "--- start: ntriples ---"
+print store.serialize(format="nt")
+print "--- end: ntriples ---\n"
diff --git a/examples/sparql_query_example.py b/examples/sparql_query_example.py
index 5a5f87fe..ed70cf7e 100644
--- a/examples/sparql_query_example.py
+++ b/examples/sparql_query_example.py
@@ -2,9 +2,9 @@ from rdflib import Literal, ConjunctiveGraph, Namespace, BNode
import rdflib
-# Note that this example uses SPARQL and assumes you have rdfextras installed
-# after installation, no other steps are nescessary, the SPARQL implementation
-# should be found automatically.
+# Note that this example uses SPARQL and assumes you have
+# rdfextras/rdflib-sparql installed, no other steps are nescessary,
+# the SPARQL implementation should be found automatically.
DC = Namespace(u"http://purl.org/dc/elements/1.1/")
FOAF = Namespace(u"http://xmlns.com/foaf/0.1/")
diff --git a/examples/swap_primer.py b/examples/swap_primer.py
index cc88bc0d..9780e425 100644
--- a/examples/swap_primer.py
+++ b/examples/swap_primer.py
@@ -7,10 +7,7 @@
# Load up RDFLib
-from rdflib import *
-from rdflib.graph import ConjunctiveGraph
-from rdflib.namespace import Namespace
-from rdflib.term import URIRef
+from rdflib import ConjunctiveGraph, Namespace, Literal
from rdflib.parser import StringInputSource
# Firstly, it doesn't have to be so complex.
@@ -22,7 +19,7 @@ myNS = Namespace('#')
primer.add((myNS.pat, myNS.knows, myNS.jo))
# or:
-primer.add((myNS['pat'], myNS['age'], long(24)))
+primer.add((myNS['pat'], myNS['age'], Literal(24)))
# Now, with just that, lets see how the system
@@ -108,7 +105,7 @@ mySource = """
# By default, your main namespace is the URI of your
# current working directory, so lets make that simpler:
-myNS = Namespace(URIRef('http://www.w3.org/2000/10/swap/Primer#'))
+myNS = Namespace('http://www.w3.org/2000/10/swap/Primer#')
primer.bind('', myNS)
primer.bind('owl', 'http://www.w3.org/2002/07/owl#')
primer.bind('dc', 'http://purl.org/dc/elements/1.1/')
diff --git a/examples/update_namespace.py b/examples/update_namespace.py
deleted file mode 100644
index 2b1553f3..00000000
--- a/examples/update_namespace.py
+++ /dev/null
@@ -1,38 +0,0 @@
-__doc__ = """Undergoing refurbishment"""
-
-
-# from rdflib import Graph, URIRef
-# #OLD = "http://www.mindswap.org/2004/terrorOnt.owl#"
-# #OLD = "http://wang-desktop/TerrorOrgInstances#"
-# OLD = "http://localhost/"
-# NEW = "http://profilesinterror.mindswap.org/"
-# graph = Graph()
-# graph.bind("terror", "http://counterterror.mindswap.org/2005/terrorism.owl#")
-# graph.bind("terror_old", "http://www.mindswap.org/2004/terrorOnt.owl#")
-# graph.bind("tech", "http://www.mindswap.org/~glapizco/technical.owl#")
-# graph.bind("wang-desk", "http://wang-desktop/TerrorOrgInstances#")
-# graph.bind("foaf", 'http://xmlns.com/foaf/0.1/')
-# graph.bind("dc", 'http://purl.org/dc/elements/1.1/')
-
-
-# REDFOOT = graph.namespace("http://redfoot.net/2005/redfoot#")
-
-# for cid, _, source in graph.triples((None, REDFOOT.source, None)):
-# if source:
-# print "updating %s" % source
-# try:
-# context = graph.get_context(cid)
-
-# for s, p, o in context:
-# context.remove((s, p, o))
-# if isinstance(s, URIRef) and OLD in s:
-# s = URIRef(s.replace(OLD, NEW))
-# if isinstance(p, URIRef) and OLD in p:
-# p = URIRef(p.replace(OLD, NEW))
-# if isinstance(o, URIRef) and OLD in o:
-# o = URIRef(o.replace(OLD, NEW))
-# context.add((s, p, o))
-
-# context.save(source, format="pretty-xml")
-# except Exception, e:
-# print(e)