Guest User

Untitled

a guest
Aug 15th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. class EventHandler:
  2. """Calls the set of functions that handle a given event.
  3.  
  4. You don't need to register events. You can EventHandler.handle('whatever')
  5. """
  6. #A tuple so that unbind and emit won't crash
  7. #bind will make it a dictionary, making it useful
  8. _eventBindings = ()
  9. def register(self, *events):
  10. if type(self._eventBindings) is tuple:
  11. self._eventBindings = {}
  12. for event in events:
  13. self._eventBindings[event] = set()
  14.  
  15. def bind(self, event, function):
  16. assert event in self._eventBindings
  17. self._eventBindings[event].add(function)
  18.  
  19. def unbind(self, event, function):
  20. assert event in self._eventBindings
  21. if function in self._eventBindings[event]:
  22. self._eventBindings[event].remove(function)
  23.  
  24. def emit(self, event, *args):
  25. assert event in self._eventBindings
  26. for function in self._eventBindings[event]:
  27. function(*args)
  28.  
  29. def __repr__(self):
  30. return '<EventHandler instance binding %s>' %str(self._eventBindings)
Add Comment
Please, Sign In to add comment