summaryrefslogtreecommitdiff
path: root/examples/custom_datatype.py
blob: 4d402793f149b4341071a1dba28e5b2116ebbe7c (plain)
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
45
46
47
48
"""
RDFLib can map between RDF 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 XSD, Graph, Literal, Namespace, term

if __name__ == "__main__":
    # Complex numbers are not registered by default
    # No custom constructor/serializer needed since
    # complex('(2+3j)') works fine
    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((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)

    # Compare with the original python complex object (should be True)
    # l2[2] is the object of the triple
    assert isinstance(l2[2], Literal)
    print(l2[2].value == c)