Skip to content

Commit be2e750

Browse files
committed
feat: add __iter__ support to LayoutElements and Elements for native iteration
1 parent 18c73ca commit be2e750

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

test_unstructured_inference/inference/test_layout_element.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from unstructured_inference.inference.layoutelement import LayoutElement, TextRegion
1+
from unstructured_inference.inference.layoutelement import LayoutElement, LayoutElements, TextRegion
2+
import numpy as np
23

34

45
def test_layout_element_do_dict(mock_layout_element):
@@ -18,3 +19,32 @@ def test_layout_element_from_region(mock_rectangle):
1819
region = TextRegion(bbox=mock_rectangle)
1920

2021
assert LayoutElement.from_region(region) == expected
22+
23+
24+
def test_layout_elements_iter_support():
25+
coords = np.array([[0, 0, 100, 100]])
26+
texts = np.array(["sample"])
27+
probs = np.array([0.9])
28+
class_ids = np.array([0])
29+
class_id_map = {0: "Text"}
30+
sources = np.array(["test_source"])
31+
text_as_html = np.array(["<p>sample</p>"])
32+
table_as_cells = np.array([None])
33+
34+
layout_elements = LayoutElements(
35+
element_coords=coords,
36+
texts=texts,
37+
element_probs=probs,
38+
element_class_ids=class_ids,
39+
element_class_id_map=class_id_map,
40+
sources=sources,
41+
text_as_html=text_as_html,
42+
table_as_cells=table_as_cells,
43+
)
44+
45+
# New feature test: __iter__() works
46+
elements = list(layout_elements)
47+
assert len(elements) == 1
48+
assert isinstance(elements[0], LayoutElement)
49+
assert elements[0].text == "sample"
50+
assert elements[0].type == "Text"

unstructured_inference/inference/elements.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ def __post_init__(self):
229229
def __getitem__(self, indices) -> TextRegions:
230230
return self.slice(indices)
231231

232+
def __iter__(self):
233+
return self.iter_elements()
234+
232235
def slice(self, indices) -> TextRegions:
233236
"""slice text regions based on indices"""
234237
return TextRegions(

unstructured_inference/inference/layoutelement.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ def __eq__(self, other: object) -> bool:
7878
def __getitem__(self, indices):
7979
return self.slice(indices)
8080

81+
def __iter__(self):
82+
return self.iter_elements()
83+
8184
def slice(self, indices) -> LayoutElements:
8285
"""slice and return only selected indices"""
8386
return LayoutElements(

0 commit comments

Comments
 (0)