Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sort iterable before passing it to itertools.groupby() #112

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion dsp_permissions_scripts/oap/oap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ def _group_oaps_together(res_oaps: list[ResourceOap], val_oaps: list[ValueOap])
oaps: list[Oap] = []
deserialized_resource_iris = []

for res_iri, _val_oaps in itertools.groupby(val_oaps, key=lambda x: x.resource_iri):
def sort_algo(x: ValueOap) -> str:
"""
According to https://docs.python.org/3/library/itertools.html#itertools.groupby,
the iterable must be sorted on the same key function, before passing it to itertools.groupby().
"""
return x.resource_iri

for res_iri, _val_oaps in itertools.groupby(sorted(val_oaps, key=sort_algo), key=sort_algo):
res_oaps_filtered = [x for x in res_oaps if x.resource_iri == res_iri]
res_oap = res_oaps_filtered[0] if res_oaps_filtered else None
oaps.append(Oap(resource_oap=res_oap, value_oaps=sorted(_val_oaps, key=lambda x: x.value_iri)))
Expand Down