-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyld-jsonld-test.py
114 lines (92 loc) · 2.54 KB
/
pyld-jsonld-test.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json, pyaml
def fmt(x):
return json.dumps(x, indent=4, sort_keys=True)
data = [
{
"@type": "kf:Gig",
"@id": "kf:000001",
"ical:summary": "Lady Gaga Concert",
"ical:dtstart": "2011-04-09T20:00Z",
"kf:venue": "kf:NYA"
},
{
"@type": "kf:Gig",
"ical:summary": "Hawkwind Concert",
"ical:dtstart": "2011-04-09T20:00Z",
"kf:venue": "kf:FPARK",
"kf:testing": { "testing2:something": "that"},
"@id": "http://dfgdsfgdfg.example/1"
},
{
"@type": "kf:Gig",
"ical:summary": "QOTSA Concert",
"ical:dtstart": "2018-07-09T20:00Z",
"kf:venue": "kf:FPARK",
"@id": "http://dfgdsfgdfg.example/2"
},
{
"@type": "kf:KLocation",
"@id": "kf:NYA",
"kf:size": 1000,
"ical:location": "New Orleans Arena, New Orleans, Louisiana, USA",
},
{
"@type": "kf:KLocation",
"@id": "kf:FPARK",
"kf:size": 2000,
"ical:location": "Finsbury Park",
}
]
context = {
"@context": {
"ical": "http://www.w3.org/2002/12/cal/ical#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"kf": "http://facta.kendra.io/vocab#",
"@vocab": "http://facta.kendra.io/vocab#",
"kf:venue": {"@type": "@id"},
"ical:dtstart": {
"@type": "xsd:dateTime"
}
}
}
frame = {
"@context": {"@vocab": "http://facta.kendra.io/vocab#",
"ical": "http://www.w3.org/2002/12/cal/ical#",
},
"@type": "Gig",
"venue":{
"@type": "KLocation"
}
}
rev_frame = {
"@context": {"@vocab": "http://facta.kendra.io/vocab#",
"ical": "http://www.w3.org/2002/12/cal/ical#",
"hosts": {"@reverse": "venue"}
},
"@type": "KLocation",
"hosts":{
"@type": "Gig"
}
}
from pyld import jsonld
compacted = jsonld.compact(data, context)
print fmt(compacted)
doc = fmt(jsonld.expand(compacted))
rdf = jsonld.normalize(compacted)
print "Normalized"
print fmt(rdf)
print "Compacted"
print fmt(jsonld.compact(jsonld.from_rdf(rdf), context))
print "Framed"
print fmt(jsonld.frame(compacted, frame))
print "Reverse framed"
print fmt(jsonld.frame(compacted, rev_frame))
# usual caveats re YAML safety here..
print "YAML"
print pyaml.dump(jsonld.frame(compacted, frame), indent=4)
exit()
from rdflib import Graph, plugin
from rdflib.serializer import Serializer
g = Graph().parse(data=doc, format='json-ld')
print g
print(g.serialize(format='nt', indent=4))