-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1463 from joshmoore/python-graph
RFC: Add PythonInputSource to create py-based graphs
- Loading branch information
Showing
3 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from rdflib import Graph | ||
from rdflib.compare import isomorphic | ||
import json | ||
|
||
|
||
def test_wrap(): | ||
""" | ||
Example of intercepting a JSON-LD structure and performing some | ||
in-memory manipulation and then passing that structure to Graph.parse | ||
lists in the shacl graph. | ||
""" | ||
|
||
_data = """ | ||
{ | ||
"@context" : { | ||
"ngff" : "http://example.com/ns#" | ||
}, | ||
"@graph": [{ | ||
"@type": "ngff:ItemList", | ||
"ngff:collectionType": {"@type": "ngff:Image"}, | ||
"ngff:itemListElement": [ | ||
{ | ||
"@type": "ngff:Image", | ||
"path": "image1", | ||
"name": "Image 1" | ||
}, | ||
{ | ||
"@type": "ngff:Image", | ||
"path": "something-else", | ||
"name": "bob" | ||
} | ||
] | ||
}] | ||
} | ||
""" | ||
|
||
# Current workaround | ||
data = json.loads(_data) | ||
data = walk(data) | ||
data = json.dumps(data) # wasteful | ||
g1 = Graph() | ||
g1.parse(data=data, format="json-ld") | ||
|
||
# Desired behavior | ||
data = json.loads(_data) | ||
data = walk(data) | ||
g2 = Graph() | ||
g2.parse(data=data, format="json-ld") | ||
|
||
assert isomorphic(g1, g2) | ||
|
||
|
||
def walk(data, path=None): | ||
""" | ||
Some arbitrary operation on a Python data structure. | ||
""" | ||
|
||
if path is None: | ||
path = [] | ||
|
||
if isinstance(data, dict): | ||
for k, v in data.items(): | ||
data[k] = walk(v, path + [k]) | ||
|
||
elif isinstance(data, list): | ||
replacement = list() | ||
for idx, item in enumerate(data): | ||
if path[-1] == "@graph": | ||
replacement.append(walk(item, path)) | ||
else: | ||
wrapper = {"@type": "ListItemWrapper", "ngff:position": idx} | ||
wrapper["ngff:item"] = walk(item, path + [idx]) | ||
replacement.append(wrapper) | ||
data = replacement | ||
|
||
return data |