From b28300d1fb9f12deb1e782af0e91061fb82358cb Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Thu, 18 Jul 2024 12:19:25 +0200 Subject: [PATCH 1/2] edit --- dsp_permissions_scripts/oap/oap_serialize.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dsp_permissions_scripts/oap/oap_serialize.py b/dsp_permissions_scripts/oap/oap_serialize.py index 990772fb..94e6fa54 100644 --- a/dsp_permissions_scripts/oap/oap_serialize.py +++ b/dsp_permissions_scripts/oap/oap_serialize.py @@ -77,7 +77,10 @@ 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: + 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))) From 7aba2f8eee44a6cfe580bf3ad60f4878c6a4aa3f Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Thu, 18 Jul 2024 13:30:20 +0200 Subject: [PATCH 2/2] add docstring --- dsp_permissions_scripts/oap/oap_serialize.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/dsp_permissions_scripts/oap/oap_serialize.py b/dsp_permissions_scripts/oap/oap_serialize.py index 94e6fa54..60fb8ac2 100644 --- a/dsp_permissions_scripts/oap/oap_serialize.py +++ b/dsp_permissions_scripts/oap/oap_serialize.py @@ -78,6 +78,10 @@ def _group_oaps_together(res_oaps: list[ResourceOap], val_oaps: list[ValueOap]) deserialized_resource_iris = [] 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):