-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_bindings.py
45 lines (33 loc) · 1.14 KB
/
data_bindings.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
class ConversionAdapter:
def __init__(self, push, pull):
self.push = push
self.pull = pull
NopAdapter = ConversionAdapter(push=lambda x: x, pull=lambda x: x)
FloatToInt = ConversionAdapter(push=lambda x: int(x), pull=lambda x: float(x))
class Binding:
def __init__(self, getter, setter = lambda: None):
self.getter = getter
self.setter = setter
def get(self):
return self.getter()
def set(self, value):
self.setter(value)
class Facet:
def __init__(self, obj, field):
self.obj = obj
self.field = field
def get(self):
return getattr(self.obj, self.field)
def set(self, value):
setattr(self.obj, self.field, value)
class DataBindings:
def __init__(self):
self.__bindings = []
def add_binding(self, source, destination, conversion = NopAdapter):
self.__bindings.append((source, destination, conversion))
def push(self):
for b in self.__bindings:
b[1].set(b[2].push(b[0].get()))
def pull(self):
for b in self.__bindings:
b[0].set(b[2].pull(b[1].get()))