-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample.py
62 lines (38 loc) · 1.92 KB
/
example.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
import json
from eventdispatcher import EventDispatcher, Property, ListProperty
class SettingsFile(EventDispatcher):
last_login = Property(None)
color = Property('red')
def __init__(self, filepath):
super(SettingsFile, self).__init__()
self.filepath = filepath
self.number_of_file_updates = 0
# Bind the properties to the function that updates the file
self.bind(last_login=self.update_settings_file,
color=self.update_settings_file)
def on_last_login(self, inst, last_login):
# Default handler for the last_login property
print 'last login was %s' % last_login
def on_color(self, inst, color):
# Default handler for the color property
print 'color has been set to %s' % color
def update_settings_file(self, *args):
# Update the file with the latest settings.
print 'Updating settings file.'
self.number_of_file_updates += 1
with open(self.filepath, 'w') as _f:
settings = {'last_login': self.last_login, 'color': self.color}
json.dump(settings, _f)
if __name__ == '__main__':
s = SettingsFile('./myfile.json')
s.last_login = 'May 18 2015' # Updates settings file
s.last_login = 'May 18 2015' # Does not update settings file
s.color = 'blue' # Updates settings file
s.color = 'blue' # Does not update settings file
print 'The file was updated %d times' % s.number_of_file_updates
file1 = SettingsFile('./myfile1.json')
file2 = SettingsFile('./myfile2.json')
# Binds the color property from file1 to the color property of file2
# Changing file1.color will automatically update the value of file2.color
file1.bind(color=file2.setter('color'))
file1.color = 'green'