Skip to content

Commit

Permalink
Merge branch 'main' into wip/dev-3883-relative-iris
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Jul 18, 2024
2 parents fb2aded + 6cb4bd6 commit 417bd1d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
repos:

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.1
rev: v0.5.2
hooks:
- id: ruff
# args: [
Expand Down
17 changes: 13 additions & 4 deletions dsp_permissions_scripts/oap/oap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,37 @@ def _read_all_oaps_from_files(
folder = _get_project_data_path(shortcode, mode)
res_oaps: list[ResourceOap] = []
val_oaps: list[ValueOap] = []
for file in folder.glob("**/*.json"):
all_files = list(folder.glob("**/*.json"))
for file in all_files:
content = file.read_text(encoding="utf-8")
if "_values_" in file.name:
val_oaps.append(ValueOap.model_validate_json(content))
else:
res_oaps.append(ResourceOap.model_validate_json(content))
logger.info(f"Read {len(res_oaps)} resource OAPs and {len(val_oaps)} value OAPs from {len(all_files)} files")
return res_oaps, val_oaps


def _group_oaps_together(res_oaps: list[ResourceOap], val_oaps: list[ValueOap]) -> list[Oap]:
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)))
deserialized_resource_iris.append(res_iri)

remaining_res_oaps = [oap for oap in res_oaps if oap.resource_iri not in deserialized_resource_iris]
for res_oap in remaining_res_oaps:
oaps.append(Oap(resource_oap=res_oap, value_oaps=[]))
oaps.extend(Oap(resource_oap=res_oap, value_oaps=[]) for res_oap in remaining_res_oaps)

oaps.sort(key=lambda oap: oap.resource_oap.resource_iri if oap.resource_oap else "")
logger.debug(f"Grouped {len(res_oaps)} resource OAPs and {len(val_oaps)} value OAPs into {len(oaps)} OAPs")
return oaps
52 changes: 26 additions & 26 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pydantic = "^2.8.2"

[tool.poetry.group.dev.dependencies]
mypy = "^1.10.1"
types-requests = "^2.32.0.20240622"
types-requests = "^2.32.0.20240712"
python-dotenv = "^1.0.1"
ruff = "^0.5.1"
ruff = "^0.5.2"
pre-commit = "^3.7.1"
pytest = "^8.2.2"
pytest-unordered = "^0.6.1"
Expand Down

0 comments on commit 417bd1d

Please sign in to comment.