Skip to content

Commit

Permalink
add list comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
brambg committed Jul 25, 2023
1 parent 332570a commit 1a9b376
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pagexml/model/physical_document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def __init__(self, points: Union[str, List[Tuple[int, int]]]):
)

self.x = min([point[0] for point in self.points])
self.y = min(point[1] for point in self.points)
self.w = max(point[0] for point in self.points) - self.x
self.y = min([point[1] for point in self.points])
self.w = max([point[0] for point in self.points]) - self.x
self.h = max([point[1] for point in self.points]) - self.y
self.type = "coords"

Expand Down
23 changes: 23 additions & 0 deletions tests/physical_document_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ def test_invalid_points(self):
with self.assertRaises(ValueError):
coords = Coords('invalid points')

def test_valid_points_from_str(self):
coords = Coords('1216,1119 1205,1109 1202,1109 1198,1112 1195,1112 1191,1116 1164,1116 1160,1119 1147,1119'
' 1143,1123 1126,1123 1123,1126 1102,1126 1098,1130 1074,1130 1071,1133 1016,1133 1012,1136'
' 964,1136 961,1140 957,1140 954,1143 940,1143 937,1147 930,1147 926,1150 916,1150 912,1154'
' 899,1154 895,1157 888,1157 885,1160 882,1160 878,1164 875,1164 857,1181 847,1181 840,1188'
' 837,1188 833,1191 830,1191 826,1195 823,1195 820,1198 816,1198 813,1202 809,1202 795,1216'
' 795,1229 799,1229 802,1233 813,1233 816,1236 875,1236 878,1240 895,1240 899,1243 923,1243'
' 926,1247 1036,1247 1040,1243 1147,1243 1150,1240 1181,1240 1185,1236 1209,1236 1212,1233'
' 1216,1233 1219,1229 1219,1226 1222,1222 1222,1216 1219,1212 1219,1209 1216,1205 1216,1150'
' 1219,1147 1219,1143 1216,1140')
x = [p[0] for p in coords.points]
print(min(x), max(x))
y = [p[1] for p in coords.points]
print(min(y), max(y))

self.assertEqual(795, coords.left)
self.assertEqual(1109, coords.top)
self.assertEqual(1222, coords.right)
self.assertEqual(1247, coords.bottom)
self.assertEqual(427, coords.width)
self.assertEqual(138, coords.height)
self.assertEqual({'x': 795, 'y': 1109, 'w': 427, 'h': 138}, coords.box)


class TestStructureDoc(unittest.TestCase):

Expand Down

0 comments on commit 1a9b376

Please sign in to comment.