Skip to content

Commit 5fde82a

Browse files
author
notactuallyfinn
committed
added contains and equals check for ld_list with tests and added issue number to fixmes
1 parent f38b662 commit 5fde82a

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

src/hermes/model/types/ld_list.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ class ld_list(ld_container):
1515
def __init__(self, data, *, parent=None, key=None, index=None, context=None):
1616
""" Create a new ld_list.py container.
1717
18-
# FIXME: there is no parameter container
18+
# FIXME: #439 there is no parameter container
1919
:param container: The container type for this list.
2020
"""
21-
# FIXME: A set container does not contain "@set" in the expected data format (expanded json ld)
21+
# FIXME: #439 A set container does not contain "@set" in the expected data format (expanded json ld)
2222
# Instead it is just a list of dicts and therefor would raise a ValueError here (and fail ld_list.is_ld_list)
2323

2424
super().__init__(data, parent=parent, key=key, index=index, context=context)
@@ -42,7 +42,7 @@ def __getitem__(self, index):
4242
return item
4343

4444
def __setitem__(self, index, value):
45-
# FIXME: what should your_ld_list[index] = [{"@type": "foo", "name": "bar"}] mean?
45+
# FIXME: #439 what should your_ld_list[index] = [{"@type": "foo", "name": "bar"}] mean?
4646
# set your_ld_list[index] to the dict {"@type": "foo", "name": "bar"} given in expanded form or
4747
# set your_ld_list[index] to the list [{"@type": "foo", "name": "bar"}] given in non expanded form or
4848
# set your_ld_list[index] to the set [{"@type": "foo", "name": "bar"}] given in expanded form
@@ -52,7 +52,7 @@ def __setitem__(self, index, value):
5252
# This is relevent because nested sets get unnested when being expanded and lists not.
5353
# Moreover a set inside a list gets automaticaly converted to a list when expanded)
5454

55-
# FIXME: what happens when a ld_list is put inside another also depends on their container types
55+
# FIXME: #439 what happens when a ld_list is put inside another also depends on their container types
5656

5757
if not isinstance(index, slice):
5858
self.item_list[index] = val[0] if isinstance(val := self._to_expanded_json(self.key, value), list) else val
@@ -74,6 +74,24 @@ def __iter__(self):
7474
item.index = index
7575
yield item
7676

77+
def __contains__(self, value):
78+
expanded_value = val[0] if isinstance(val := self._to_expanded_json(self.key, value), list) else val
79+
return expanded_value in self.item_list
80+
81+
def __eq__(self, other):
82+
if isinstance(other, ld_list):
83+
# FIXME: #439 When are ld_lists equal?
84+
return self.item_list == other.item_list and self.container == other.container
85+
if isinstance(other, list):
86+
return self.item_list == self._to_expanded_json(self.key, other)[0]["@list"]
87+
return NotImplemented
88+
89+
def __ne__(self, other):
90+
x = self.__eq__(other)
91+
if x is NotImplemented:
92+
return NotImplemented
93+
return not x
94+
7795
def append(self, value):
7896
ld_value = self._to_expanded_json(self.key, value)
7997
self.item_list.extend(ld_value)
@@ -90,12 +108,12 @@ def to_python(self):
90108

91109
@classmethod
92110
def is_ld_list(cls, ld_value):
93-
# FIXME: every python list that contains at least one dict can be considerd a set in expanded json form
111+
# FIXME: #439 every python list that contains at least one dict can be considerd a set in expanded json form
94112
return cls.is_ld_node(ld_value) and cls.is_container(ld_value[0])
95113

96114
@classmethod
97115
def is_container(cls, value):
98-
# FIXME: "@set" will never be inside a dictionary of an expanded json ld object
116+
# FIXME: #439 "@set" will never be inside a dictionary of an expanded json ld object
99117
return (
100118
isinstance(value, dict)
101119
and len([1 for ct in cls.container_types if isinstance(value.get(ct, None), list)]) == 1

test/hermes_test/model/types/test_ld_list.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,31 @@ def test_append():
107107
assert 2 * li[4].item_list == 2 * [li[5].data_dict] == li[6].item_list
108108

109109

110+
def test_build_in_contains():
111+
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
112+
li.append("foo")
113+
li.append({"@type": "A", "schema:name": "a"})
114+
assert "foo" in li and {"@type": "A", "schema:name": "a"} in li
115+
assert {"@value": "foo"} in li and {"@type": "A", "https://schema.org/name": "a"} in li
116+
117+
118+
def test_build_in_comparison():
119+
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
120+
li2 = ld_list([{"@set": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
121+
li3 = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema2": "https://schema.org/"}])
122+
assert li == [] and li2 == [] and [] == li and [] == li2
123+
assert li != li2 and li == li3
124+
li.append("foo")
125+
li.append({"@type": "A", "schema:name": "a"})
126+
assert li != li3 and ["foo", {"@type": "A", "schema:name": "a"}] == li and ["foo"] != li3
127+
assert ["foo", {"@type": "A", "https://schema.org/name": "a"}] == li
128+
li3.extend(["foo", {"@type": "A", "schema2:name": "a"}])
129+
assert li == li3
130+
li4 = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
131+
li4.extend([{"@type": "A", "schema:name": "a"}, "foo"])
132+
assert li != li4
133+
134+
110135
def test_extend():
111136
li = ld_list([{"@list": []}], key="https://schema.org/name", context=[{"schema": "https://schema.org/"}])
112137
li.extend([])

0 commit comments

Comments
 (0)