+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 22 additions & 1 deletion lib/sycamore/sycamore/tests/unit/data/test_element.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sycamore.data.element import create_element, Element, ImageElement
from sycamore.data.element import create_element, Element, ImageElement, TableElement
from sycamore.data.table import Table, TableCell


def test_create_element_bad_type():
Expand All @@ -10,3 +11,23 @@ def test_create_element_bad_type():

e = create_element(type="iMaGE")
assert isinstance(e, ImageElement)


def test_field_to_value_table():
table = Table(
[
TableCell(content="head1", rows=[0], cols=[0], is_header=True),
TableCell(content="head2", rows=[0], cols=[1], is_header=True),
TableCell(content="3", rows=[1], cols=[0], is_header=False),
TableCell(content="4", rows=[1], cols=[1], is_header=False),
]
)

elem = create_element(type="table", table=table, properties={"parent": {"child1": 1, "child2": 2}})
assert isinstance(elem, TableElement)

assert elem.field_to_value("properties.parent.child1") == 1
assert elem.field_to_value("properties.parent.child2") == 2
assert elem.field_to_value("properties.parent.child3") is None

assert elem.field_to_value("text_representation") == "head1,head2\n3,4\n"
11 changes: 9 additions & 2 deletions lib/sycamore/sycamore/utils/nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ def nested_lookup(d: Any, keys: list[str]) -> Any:
if d is None:
return None
try:
d = d.get(keys[0])
except AttributeError:
if isinstance(keys[0], str) and hasattr(d, keys[0]):
# This is necessary to handle attributes with a property
# getter that returns something different than what's in the
# underlying dict. For example the text_representation for
# a TableElement.
d = getattr(d, keys[0])
else:
d = d.get(keys[0])
except (AttributeError, ValueError):
return None

keys = keys[1:]
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载