Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
da136c3
changed ld_list according to issue #439
Oct 17, 2025
03ebd32
implemented tests for problem 2 of the issue and more tests
Oct 20, 2025
d1eb612
added a few missing conversions and formatted the file a little bit d…
Oct 20, 2025
d28dc10
removed one conversion, added one and changed _to_python to fix conve…
Oct 20, 2025
fb5aa64
fixed formatting errors
Oct 20, 2025
5248853
fixed ld_container tests, a type conversion and equals of ld_list
Oct 24, 2025
e2c1bba
added detailed comparison for ld_list, ld_dict and json_values and ad…
Nov 7, 2025
aeb88fb
added support for graph and set again and improved expansion of json-ld
Nov 21, 2025
007e3a3
removed unnecessary lambda
Nov 21, 2025
3c9ea61
fixed set and from_list and added del
Nov 21, 2025
66e03df
updated tests of ld_container and fixed found bugs
Nov 21, 2025
c4aea65
pacified flake8
Nov 21, 2025
7d30b16
improved ld_list.__contains__ and added tests for it
Nov 24, 2025
f8d259f
added one TODO item to ld_list
Nov 24, 2025
2b98627
began adding doc strings, type hints and comments for ld_container
Nov 24, 2025
1958518
improved _to_expanded_json
Nov 28, 2025
4acf8de
added doc-strings and comments and pacified flake8
Nov 28, 2025
b751130
began commenting and adding doc-strings to ld_list
Nov 28, 2025
3c243f2
fixed bug in _to_expanded_json
Dec 5, 2025
e0f0fc9
added a few test cases to delete of ld_list
Dec 5, 2025
1b5656e
added more comments to ld_list
Dec 5, 2025
071630c
added more doc strings
Dec 5, 2025
d4b34b4
corrected type hints
Dec 8, 2025
f0f1818
documented the rest of the methods in ld_container
Dec 8, 2025
bcc233d
implemented list comparison and added tests for it
Dec 12, 2025
287d37a
added tests and fixed small bug and fixed typos
Dec 15, 2025
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
51 changes: 22 additions & 29 deletions src/hermes/model/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

# SPDX-FileContributor: Michael Meinel
# SPDX-FileContributor: Michael Fritzsche

from datetime import date, time, datetime

Expand All @@ -21,56 +22,48 @@
"ld_container": lambda c, **_: c,
"json": lambda c, **_: c.compact(),
"expanded_json": lambda c, **_: c.ld_value,
},
}
),

# Wrap item from ld_dict in ld_list
(ld_list.is_ld_list, dict(ld_container=ld_list)),
(
lambda c: isinstance(c, list) and all(isinstance(item, dict) for item in c),
dict(ld_container=lambda c, **kw: ld_list([{"@list": c}], **kw))
),
(ld_list.is_ld_list, {"ld_container": ld_list}),
(lambda c: isinstance(c, list), {"ld_container": lambda c, **kw: ld_list(c, **kw)}),

# pythonize items from lists (expanded set is already handled above)
(ld_container.is_json_id, dict(python=lambda c, **_: c["@id"])),
(ld_container.is_typed_json_value, dict(python=ld_container.typed_ld_to_py)),
(ld_container.is_json_value, dict(python=lambda c, **_: c["@value"])),
(ld_list.is_container, dict(ld_container=lambda c, **kw: ld_list([c], **kw))),
(ld_dict.is_json_dict, dict(ld_container=lambda c, **kw: ld_dict([c], **kw))),
(ld_container.is_json_id, {"python": lambda c, **_: c["@id"]}),
(ld_container.is_typed_json_value, {"python": lambda c, **kw: ld_container.typed_ld_to_py([c], **kw)}),
(ld_container.is_json_value, {"python": lambda c, **_: c["@value"]}),
(ld_list.is_container, {"ld_container": lambda c, **kw: ld_list([c], **kw)}),
(ld_dict.is_json_dict, {"ld_container": lambda c, **kw: ld_dict([c], **kw)}),
(lambda v: isinstance(v, str), {"python": lambda v, parent, **_: parent.ld_proc.compact_iri(parent.active_ctx, v)}),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rule is to generic IMHO, as it will try to compact all strings that are encountered, even those that are explicitly not meant as representing an IRI.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But shouldn't all other string should be known to be a value of some sort i.e. formed like {"@value": _} and therefor filtered by is_json_value because we get valid JSON-LD?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you have answers... but I'm totally not sure whether this is really true.

Apart from this, except for @type values, all expansible values should be wrapped in @id nodes... maybe this is a good point to check whether this assumption is correct. The other question is, which kind of expansion is required (i.e., vocab=True or not... see discussion about prefix, namespace, IRIs from the past)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... anyways, there should most proably be a documentation for the conversion rules and the order they are applied in (maybe directly generated from the code... ;) )


# Convert internal data types to expanded_json
(lambda c: ld_container.is_json_id(c) or ld_container.is_json_value(c), dict(expanded_json=lambda c, **_: [c])),
(ld_dict.is_json_dict, dict(expanded_json=lambda c, **kw: ld_dict.from_dict(c, **kw).ld_value)),
(ld_dict.is_ld_dict, dict(expanded_json=lambda c, **kw: ld_dict.from_dict(c[0], **kw).ld_value)),
(ld_container.is_json_id, {"expanded_json": lambda c, **_: [c]}),
(ld_container.is_ld_id, {"expanded_json": lambda c, **_: c}),
(ld_container.is_json_value, {"expanded_json": lambda c, **_: [c]}),
(ld_container.is_ld_value, {"expanded_json": lambda c, **_: c}),
(ld_dict.is_json_dict, {"expanded_json": lambda c, **kw: ld_dict.from_dict(c, **kw).ld_value}),
(
ld_list.is_container,
dict(
expanded_json=lambda c, **kw: ld_list.from_list(
ld_list([c]).item_list, container=ld_list([c]).container, **kw
).ld_value
),
{"expanded_json": lambda c, **kw: ld_list.from_list(ld_list.get_item_list_from_container(c), **kw).ld_value}
),
(
ld_list.is_ld_list,
dict(
expanded_json=lambda c, **kw: ld_list.from_list(
ld_list(c).item_list, container=ld_list(c).container, **kw
).ld_value
),
{"expanded_json": lambda c, **kw: ld_list.from_list(ld_list.get_item_list_from_container(c[0]), **kw).ld_value}
),
(lambda c: isinstance(c, list), dict(expanded_json=lambda c, **kw: ld_list.from_list(c, **kw).ld_value)),
(lambda v: isinstance(v, (int, float, str, bool)), dict(expanded_json=lambda v, **_: [{"@value": v}])),
(lambda c: isinstance(c, list), {"expanded_json": lambda c, **kw: ld_list.from_list(c, **kw).ld_value}),
(lambda v: isinstance(v, (int, float, str, bool)), {"expanded_json": lambda v, **_: [{"@value": v}]}),
(
lambda v: isinstance(v, datetime),
dict(expanded_json=lambda v, **_: [{"@value": v.isoformat(), "@type": iri_map["schema:DateTime"]}]),
{"expanded_json": lambda v, **_: [{"@value": v.isoformat(), "@type": iri_map["schema:DateTime"]}]}
),
(
lambda v: isinstance(v, date),
dict(expanded_json=lambda v, **_: [{"@value": v.isoformat(), "@type": iri_map["schema:Date"]}]),
{"expanded_json": lambda v, **_: [{"@value": v.isoformat(), "@type": iri_map["schema:Date"]}]}
),
(
lambda v: isinstance(v, time),
dict(expanded_json=lambda v, **_: [{"@value": v.isoformat(), "@type": iri_map["schema:Time"]}]),
{"expanded_json": lambda v, **_: [{"@value": v.isoformat(), "@type": iri_map["schema:Time"]}]}
),
]

Expand Down
Loading
Loading