Created
June 24, 2011 23:21
-
-
Save smoak/1045874 to your computer and use it in GitHub Desktop.
Event Hooks for python
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 EventHook(object): | |
def __init__(self): | |
self.__handlers = [] | |
def addHandler(self, handler): | |
self.__handlers.append(handler) | |
def removeHandler(self, handler): | |
self.__handlers.remove(handler) | |
def fire(self, *args, **kwargs): | |
for handler in self.__handlers: | |
handler(*args, **kwargs) | |
def clearObjectHandlers(self, inObject): | |
for theHandler in self.__handlers: | |
if theHandler.im_self == inObject: | |
self.removeHandler(theHandler) | |
class Pizza: | |
def __init__(self): | |
self.ingredients = [] | |
class Baker: | |
def __init__(self): | |
self.onPizzaReady = EventHook() | |
def makePizza(self): | |
p = Pizza() | |
self.onPizzaReady.fire(pizza=p) | |
def pizzaDone(pizza): | |
print "Pizza is done!" | |
print "Ingredients %d " % (len(pizza.ingredients)) | |
def main(): | |
b = Baker() | |
b.onPizzaReady.addHandler(pizzaDone) | |
b.makePizza() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment