/
container.py
70 lines (53 loc) · 1.71 KB
/
container.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from itertools import chain
from . import utils, convert
class Container(object):
cached_properties = ["_rect_edges", "_edges", "_objects"]
def flush_cache(self, properties=None):
props = self.cached_properties if properties is None else properties
for p in props:
if hasattr(self, p):
delattr(self, p)
@property
def rects(self):
return self.objects.get("rect", [])
@property
def lines(self):
return self.objects.get("line", [])
@property
def curves(self):
return self.objects.get("curve", [])
@property
def images(self):
return self.objects.get("image", [])
@property
def figures(self):
return self.objects.get("figure", [])
@property
def chars(self):
return self.objects.get("char", [])
@property
def rect_edges(self):
if hasattr(self, "_rect_edges"):
return self._rect_edges
rect_edges_gen = (utils.rect_to_edges(r) for r in self.rects)
self._rect_edges = list(chain(*rect_edges_gen))
return self._rect_edges
@property
def edges(self):
if hasattr(self, "_edges"):
return self._edges
line_edges = list(map(utils.line_to_edge, self.lines))
self._edges = self.rect_edges + line_edges
return self._edges
@property
def horizontal_edges(self):
def test(x):
return x["orientation"] == "h"
return list(filter(test, self.edges))
@property
def vertical_edges(self):
def test(x):
return x["orientation"] == "v"
return list(filter(test, self.edges))
Container.to_json = convert.to_json
Container.to_csv = convert.to_csv