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

New TableDoc class providing a table interface for data documentation #273

Merged
merged 15 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed doctest issue
  • Loading branch information
jesper-friis committed Dec 15, 2024
commit ef5239ad7e96f586c5ce970cec834cf7881864ff
17 changes: 11 additions & 6 deletions tripper/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
from pathlib import Path
from typing import TYPE_CHECKING

import requests
import yaml # type: ignore

from tripper import DCAT, EMMO, OTEIO, OWL, RDF, Triplestore
from tripper.utils import AttrDict, as_python

Expand Down Expand Up @@ -171,6 +168,8 @@
- data models (require that DLite is installed)

"""
import requests

# Save statements and mappings
statements = get_values(dct, "statements")
statements.extend(get_values(dct, "mappings"))
Expand Down Expand Up @@ -351,6 +350,8 @@
fromfile: Whether to load the context from local file.

"""
import requests

if fromfile:
with open(CONTEXT_PATH[7:], "r", encoding="utf-8") as f:
context = json.load(f)["@context"]
Expand Down Expand Up @@ -436,16 +437,18 @@
)


def addnested(d: "Union[dict, list]", key: str, value: "Any"):
def addnested(
d: "Union[dict, list]", key: str, value: "Any"
) -> "Union[dict, list]":
"""Like add(), but allows `key` to be a dot-separated list of sub-keys.
Returns the updated `d`.

Each sub-key will be added to `d` as a corresponding sub-dict.

Example:

>>> d = {}
>>> addnested(d, "a.b.c", "val")
>>> d == {'a': {'b': {'c': 'val'}}}
>>> addnested(d, "a.b.c", "val") == {'a': {'b': {'c': 'val'}}}
True

"""
Expand All @@ -457,18 +460,18 @@
addnested(ele, key, value)
break
else:
d.append(addnested({}, key, value))

Check warning on line 463 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L463

Added line #L463 was not covered by tests
elif first in d and isinstance(d[first], (dict, list)):
addnested(d[first], rest, value)
else:
addnested(d, first, addnested(AttrDict(), rest, value))
elif isinstance(d, list):
for ele in d:
if isinstance(ele, dict):
add(ele, key, value)
break

Check warning on line 472 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L469-L472

Added lines #L469 - L472 were not covered by tests
else:
d.append({key: value})

Check warning on line 474 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L474

Added line #L474 was not covered by tests
else:
add(d, key, value)
return d
Expand Down Expand Up @@ -508,6 +511,8 @@

def read_datadoc(filename: "Union[str, Path]") -> dict:
"""Read YAML data documentation and return it as a dict."""
import yaml # type: ignore

with open(filename, "r", encoding="utf-8") as f:
d = yaml.safe_load(f)
return prepare_datadoc(d)
Expand Down Expand Up @@ -619,20 +624,20 @@
d.update(dct)
add(d, "@type", t) # readd type if overwritten
else:
d.update(dct)

Check warning on line 627 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L627

Added line #L627 was not covered by tests

for k, v in kwargs.items():
key = f"@{k[1:]}" if re.match("^_([^_]|([^_].*[^_]))$", k) else k
add(d, key, v)

if "@id" not in d and not _entryid:
raise ValueError("Missing '@id' in dict to document")

Check warning on line 634 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L634

Added line #L634 was not covered by tests

if not _entryid:
_entryid = d["@id"]

if "@type" not in d:
warnings.warn(f"Missing '@type' in dict to document: {_entryid}")

Check warning on line 640 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L640

Added line #L640 was not covered by tests

all_prefixes = get_prefixes()
if prefixes:
Expand Down Expand Up @@ -667,7 +672,7 @@
if isinstance(e, str):
v[i] = expand_iri(e, all_prefixes)
elif isinstance(e, dict) and k in nested:
v[i] = as_jsonld(

Check warning on line 675 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L675

Added line #L675 was not covered by tests
e, k, _entryid=_entryid, prefixes=prefixes
)
elif isinstance(v, dict) and k in nested:
Expand Down
Loading