Created
April 23, 2015 15:22
-
-
Save gdugas/576a1cff338e15b1d6c8 to your computer and use it in GitHub Desktop.
Simple event dispatcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Event(object): | |
def __init__(self): | |
self.callables = {} | |
def on(self, func, uid=None): | |
if not callable(func): | |
raise TypeError("callable required") | |
if not uid: | |
uid = str(hash(func)) | |
self.callables[uid] = func | |
def off(self, func): | |
if type(func) == str: | |
del self.callables[func] | |
else: | |
del self.callables[str(hash(func))] | |
def fire(self, *args, **kwargs): | |
for key in self.callables: | |
func = self.callables[key] | |
yield func(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment