-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
namespace.py
39 lines (32 loc) · 1.17 KB
/
namespace.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
from collections import defaultdict
class InstrumentedNamespace(dict):
def __init__(self, *args, **kwargs):
"""
Set up a namespace id
"""
dict.__init__(self,*args,**kwargs)
self.events = defaultdict(lambda: defaultdict(list))
def on(self, key, event, f):
self.events[key][event].append(f)
def off(self, key, event=None, f=None):
if event is None:
self.events.pop(key, None)
elif f is None:
self.events[key].pop(event, None)
else:
self.events[key][event].remove(f)
def trigger(self, key, event, *args, **kwargs):
if key in self.events and event in self.events[key]:
for f in self.events[key][event]:
f(key, *args, **kwargs)
def __setitem__(self, key, value):
"""
Set a value in the dictionary and run attached notification functions.
"""
if key not in self:
self.trigger(key, 'initialize', value)
dict.__setitem__(self, key, value)
self.trigger(key, 'change', value)
def __delitem__(self, key):
dict.__delitem__(self, key)
self.off(key)