/usr/share/doc/python-rdflib-doc/examples/custom_datatype.py is in python-rdflib-doc 4.1.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | """
RDFLib can map between data-typed literals and python objects.
Mapping for integers, floats, dateTimes, etc. are already added, but
you can also add your own.
This example shows how :meth:`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
if __name__=='__main__':
# 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:")
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
|