Ian Lewis  committed 75923c1

Improved handler code to be a bit more efficient

  • Participants
  • Parent commits fbc35a6

Comments (0)

Files changed (1)

File skypehub/

+from collections import defaultdict
 from select import select
 from time import time
 from struct import pack, unpack
     default_receivers = ()
     def __init__(self, skype=None):
-        self.receivers = list(self.default_receivers)
+        self.receivers = defaultdict(lambda: set()) 
+        for status in self.default_statuses:
+            for receiver in self.default_receivers:
+                self.receivers[status].add(receiver) = skype
     def connect(self, receiver, statuses=None):
         if statuses is None:
             statuses = list(self.default_statuses)
-        if not receiver in self.receivers:
-            self.receivers.append((statuses, receiver))
+        for status in statuses:
+            self.receivers[status].add(receiver)
     def dispatch(self, message, status):
-        for statuses, receiver in self.receivers:
-            if status in statuses:
-                receiver(self, message, status)
+        for receiver in self.receivers[status]:
+            receiver(self, message, status)
 class OnTimeHandler(object):