-
Notifications
You must be signed in to change notification settings - Fork 2
/
Model.py
67 lines (53 loc) · 1.61 KB
/
Model.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
import pydiagrams.baseItems as bi
class Item(bi.BaseItem):
@property
def Id(self):
if self.id:
return self.id
else:
return self.label.replace(' ', '_')
@Id.setter
def Id(self, value):
self.id = value
# Model
class Model(bi.BaseItemCollection):
pass
class Context():
""" Represents a general data model. Implements as a context """
def __init__(self, model):
self.model = model
def __enter__(self):
return self.model
def __exit__(self, *args):
self.model.set_ids()
# A class for mantaining many-many relationships between two objects
# the classes are A and B
class ManyToMany(dict):
class Index(dict):
def __setitem__(self, key, value):
if key in self:
self[key].append(value)
else:
super().__setitem__(key, [value])
def __init__(self):
super().__init__({})
self._A = ManyToMany.Index()
self._B = ManyToMany.Index()
def get(self, a, b):
return self.__getitem__((a,b))
def set(self, a, b, value=None):
super().__setitem__( (a,b), value)
self._A[a] = b
self._B[b] = a
def get_by_A(self, a):
""" Returns the set of B objects for an A """
return self._A.__getitem__(a)
def get_by_B(self, b):
""" Returns the set of A objects for a B """
return self._B.__getitem__(b)
def set_by_A(self, a, b_list):
for b in b_list:
self.set(a, b)
def set_by_B(self, b, a_list):
for a in a_list:
self.set(a, b)