improved comments & printouts
@@ -10,32 +10,40 @@ mappings between literal datatypes and Python objects
from rdflib import Graph, Literal, Namespace, XSD
-from rdflib.term import bind
+from rdflib import term
if __name__ == "__main__":
- # complex numbers are not registered by default
- # no custom constructor/serializer needed since
+ # Complex numbers are not registered by default
+ # No custom constructor/serializer needed since
# complex('(2+3j)') works fine
- bind(XSD.complexNumber, complex)
-
- ns = Namespace("urn:my:namespace:")
+ term.bind(XSD.complexNumber, complex)
+ # Create a complex number RDFlib Literal
+ EG = Namespace("http://example.com/")
c = complex(2, 3)
-
l = Literal(c)
+ # Add it to a graph
g = Graph()
- g.add((ns.mysubject, ns.myprop, l))
-
- n3 = g.serialize(format="n3")
-
- # round-trip through n3 serialize/parse
- g2 = Graph()
- g2.parse(data=n3, format="n3")
-
- l2 = list(g2)[0][2]
-
+ g.add((EG.mysubject, EG.myprop, l))
+ # Print the triple to see what it looks like
+ print(list(g)[0])
+ # prints: (
+ # rdflib.term.URIRef('http://example.com/mysubject'),
+ # rdflib.term.URIRef('http://example.com/myprop'),
+ # rdflib.term.Literal(
+ # '(2+3j)',
+ # datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#complexNumber')
+ # )
+ # )
+
+ # Round-trip through n3 serialize/parse
+ g2 = Graph().parse(data=g.serialize())
+
+ l2 = list(g2)[0]
print(l2)
- print(l2.value == c) # back to a python complex object
+ # Compare with the original python complex object (should be True)
+ # l2[2] is the object of the triple
+ print(l2[2].value == c)
