-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablemap.py
53 lines (43 loc) · 1.61 KB
/
tablemap.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
import pickle
class TablemapArea:
def __init__(self, id, x, y, w, h, label, is_bool_symbol=False):
self.id = id
self.x = x
self.y = y
self.w = w
self.h = h
self.label = label
self.is_bool_symbol = is_bool_symbol
def to_json(self):
bool_val = 0
if self.is_bool_symbol:
bool_val = 1
return f'{{"x": {self.x}, "y": {self.y}, "w": {self.w}, "h": {self.h}, "label": "{self.label}", "is_bool_symbol": {bool_val}}}'
class Tablemap:
def __init__(self):
self.tablemap_areas = []
def add(self, x, y, w, h, label, is_bool_symbol=False):
id = len(self.tablemap_areas)
self.tablemap_areas.append(TablemapArea(id, x, y, w, h, label, is_bool_symbol))
self.tablemap_areas = sorted(self.tablemap_areas, key=lambda x: x.label)
def remove(self, label):
new_tablemap_areas = []
for area in self.tablemap_areas:
if area.label != label:
new_tablemap_areas.append(area)
self.tablemap_areas = new_tablemap_areas
def to_json(self):
json = '[\n'
for i, tablemap_area in enumerate(self.tablemap_areas):
json += tablemap_area.to_json()
if i != len(self.tablemap_areas) - 1:
json += ','
json += '\n'
json += ']'
return json
@staticmethod
def load(pickle_path):
return pickle.load(open(pickle_path, 'rb'))
@staticmethod
def dump(tablemap, pickle_path):
pickle.dump(tablemap, open(pickle_path, 'wb'))