Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit 940e0cc

Browse files
committed
[REF]: Table set edges
1. **Dedicated Methods**: The code has been split into several dedicated methods: - `_set_vertical_edges`: Handles the logic for setting vertical edges. - `_update_vertical_edges`: Updates the edges for vertical lines based on the computed indices. - `_set_horizontal_edges`: Handles the logic for setting horizontal edges. - `_update_horizontal_edges`: Updates the edges for horizontal lines based on the computed indices. - `_find_close_point`: A helper method to find the closest point and has been moved outside of `set_edges` for better organization. 2. **Reduced Complexity**: Each method now has a clear purpose, which reduces the complexity of the `set_edges` method itself. This makes it easier to read and understand. 3. **Maintainability**: With the separate methods for setting vertical and horizontal edges, any changes to that logic can be made in isolation. 4. **Return Value**: The method still returns `self`, maintaining the original functionality and allowing for method chaining if desired.
1 parent 9b1d9af commit 940e0cc

File tree

1 file changed

+46
-37
lines changed

1 file changed

+46
-37
lines changed

camelot/core.py

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -655,60 +655,69 @@ def set_edges(self, vertical, horizontal, joint_tol=2):
655655
horizontal : list
656656
List of detected horizontal lines.
657657
joint_tol : int, optional
658-
[description], by default 2
658+
Tolerance for determining proximity, by default 2
659659
"""
660+
self._set_vertical_edges(vertical, joint_tol)
661+
self._set_horizontal_edges(horizontal, joint_tol)
662+
return self
660663

661-
def find_close_point(over, coord, joint_tol):
662-
for i, t in enumerate(over):
663-
if math.isclose(coord, t[0], abs_tol=joint_tol):
664-
return i
665-
return None
664+
def _find_close_point(self, coords, coord, joint_tol):
665+
for i, t in enumerate(coords):
666+
if math.isclose(coord, t[0], abs_tol=joint_tol):
667+
return i
668+
return None
666669

670+
def _set_vertical_edges(self, vertical, joint_tol):
667671
for v in vertical:
668672
# find closest x coord
669673
# iterate over y coords and find closest start and end points
670-
start = find_close_point(self.rows, v[3], joint_tol)
674+
start = self._find_close_point(self.rows, v[3], joint_tol)
671675
if start is None:
672676
continue
673-
end = find_close_point(self.rows, v[1], joint_tol)
677+
end = self._find_close_point(self.rows, v[1], joint_tol)
674678
if end is None:
675679
end = len(self.rows)
676-
i = find_close_point(self.cols, v[0], joint_tol)
677-
if i is None: # only right edge
678-
i = len(self.cols) - 1
679-
for j in range(start, end):
680-
self.cells[j][i].right = True
681-
elif i == 0: # only left edge
682-
for j in range(start, end):
683-
self.cells[j][0].left = True
684-
else: # both left and right edges
685-
for j in range(start, end):
686-
self.cells[j][i].left = True
687-
self.cells[j][i - 1].right = True
688-
680+
i = self._find_close_point(self.cols, v[0], joint_tol)
681+
self._update_vertical_edges(start, end, i)
682+
683+
def _update_vertical_edges(self, start, end, index):
684+
if index is None: # only right edge
685+
index = len(self.cols) - 1
686+
for j in range(start, end):
687+
self.cells[j][index].right = True
688+
elif index == 0: # only left edge
689+
for j in range(start, end):
690+
self.cells[j][0].left = True
691+
else: # both left and right edges
692+
for j in range(start, end):
693+
self.cells[j][index].left = True
694+
self.cells[j][index - 1].right = True
695+
696+
def _set_horizontal_edges(self, horizontal, joint_tol):
689697
for h in horizontal:
690698
# find closest y coord
691699
# iterate over x coords and find closest start and end points
692-
start = find_close_point(self.cols, h[0], joint_tol)
700+
start = self._find_close_point(self.cols, h[0], joint_tol)
693701
if start is None:
694702
continue
695-
end = find_close_point(self.cols, h[2], joint_tol)
703+
end = self._find_close_point(self.cols, h[2], joint_tol)
696704
if end is None:
697705
end = len(self.cols)
698-
i = find_close_point(self.rows, h[1], joint_tol)
699-
if i is None: # only bottom edge
700-
i = len(self.rows) - 1
701-
for j in range(start, end):
702-
self.cells[i][j].bottom = True
703-
elif i == 0: # only top edge
704-
for j in range(start, end):
705-
self.cells[0][j].top = True
706-
else: # both top and bottom edges
707-
for j in range(start, end):
708-
self.cells[i][j].top = True
709-
self.cells[i - 1][j].bottom = True
710-
711-
return self
706+
i = self._find_close_point(self.rows, h[1], joint_tol)
707+
self._update_horizontal_edges(start, end, i)
708+
709+
def _update_horizontal_edges(self, start, end, index):
710+
if index is None: # only bottom edge
711+
index = len(self.rows) - 1
712+
for j in range(start, end):
713+
self.cells[index][j].bottom = True
714+
elif index == 0: # only top edge
715+
for j in range(start, end):
716+
self.cells[0][j].top = True
717+
else: # both top and bottom edges
718+
for j in range(start, end):
719+
self.cells[index][j].top = True
720+
self.cells[index - 1][j].bottom = True
712721

713722
def set_border(self):
714723
"""Sets table border edges to True."""

0 commit comments

Comments
 (0)