Skip to content

Commit cbc9e1b

Browse files
authored
Adding functionality to sort collections by ID (#45)
* adding_sort * test * correction test
1 parent 88853ec commit cbc9e1b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

pipeline/src/collection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ def _get_blank_node_identifier(self):
6262
identifier = len(self.nodes)
6363
return fmt.format(identifier=identifier)
6464

65+
def _sort_nodes_by_id(self):
66+
sorted_nodes=dict(sorted(self.nodes.items()))
67+
self.nodes=sorted_nodes
68+
69+
6570
def save(self, path, individual_files=False, include_empty_properties=False):
6671
"""
6772
Save the node collection to disk in JSON-LD format.
@@ -102,6 +107,7 @@ def save(self, path, individual_files=False, include_empty_properties=False):
102107
parent_dir = os.path.dirname(path)
103108
if parent_dir:
104109
os.makedirs(parent_dir, exist_ok=True)
110+
self._sort_nodes_by_id()
105111
data = {
106112
"@context": data_context,
107113
"@graph": [
@@ -121,6 +127,7 @@ def save(self, path, individual_files=False, include_empty_properties=False):
121127
raise OSError(
122128
f"If saving to multiple files, `path` must be a directory. path={path}, pwd={os.getcwd()}"
123129
)
130+
self._sort_nodes_by_id()
124131
output_paths = []
125132
for node in self:
126133
if node.id.startswith("http"):

pipeline/tests/test_collections.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import os.path
66
import shutil
7+
import json
78

89
from openminds.collection import Collection
910
import openminds.latest.controlled_terms
@@ -74,3 +75,60 @@ def test_round_trip_multi_file():
7475
p = person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
7576
np = new_person.to_jsonld(include_empty_properties=False, embed_linked_nodes=True)
7677
assert p == np
78+
79+
80+
def test_collection_sort_by_id():
81+
person = omcore.Person(given_name="A", family_name="Professor", id="_:004")
82+
uni1 = omcore.Organization(full_name="University of This Place", id="_:002")
83+
uni2 = omcore.Organization(full_name="University of That Place", id="_:001")
84+
person.affiliations = [
85+
omcore.Affiliation(member_of = uni1),
86+
omcore.Affiliation(member_of = uni2),
87+
]
88+
89+
c = Collection(person,uni1,uni2)
90+
output_paths = c.save("test_collection_sort_by_id.jsonld", individual_files=False, include_empty_properties=False)
91+
92+
assert output_paths == ["test_collection_sort_by_id.jsonld"]
93+
94+
with open(output_paths[0]) as fp:
95+
saved_data = json.load(fp)
96+
os.remove("test_collection_sort_by_id.jsonld")
97+
98+
expected_saved_data={
99+
"@context": {"@vocab": "https://openminds.om-i.org/props/"},
100+
"@graph": [
101+
{
102+
"@id": "_:001",
103+
"@type": "https://openminds.om-i.org/types/Organization",
104+
"fullName": "University of That Place"
105+
},
106+
{
107+
"@id": "_:002",
108+
"@type": "https://openminds.om-i.org/types/Organization",
109+
"fullName": "University of This Place"
110+
},
111+
{
112+
"@id": "_:004",
113+
"@type": "https://openminds.om-i.org/types/Person",
114+
"affiliation": [
115+
{
116+
"@type": "https://openminds.om-i.org/types/Affiliation",
117+
"memberOf": {
118+
"@id": "_:002"
119+
}
120+
},
121+
{
122+
"@type": "https://openminds.om-i.org/types/Affiliation",
123+
"memberOf": {
124+
"@id": "_:001"
125+
}
126+
}
127+
],
128+
"familyName": "Professor",
129+
"givenName": "A"
130+
}
131+
]
132+
}
133+
134+
assert saved_data == expected_saved_data

0 commit comments

Comments
 (0)