-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslot.py
34 lines (28 loc) · 781 Bytes
/
slot.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
'''
Extremely basic signal/slot class
Just store a dict of ints to callbacks.
'''
class Slot(object):
'''
Stores a list of callables, and calls them with the
args passed to self.signal.
'''
def __init__(self):
self.d = {} # {int: callable}
self.n = 0 # counter for keys
def add(self, fn):
key = self.n
self.n += 1
self.d[key] = fn
return key
def remove(self, handle):
try:
del self.d[handle]
except KeyError:
pass
def signal(self, *args, **kwargs):
# make a copy of the dict and iterate through
# that, so callbacks can remove themselves
copy = dict(self.d)
for fn in copy.itervalues():
fn(*args, **kwargs)