Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EventHandler:
- """Calls the set of functions that handle a given event.
- You don't need to register events. You can EventHandler.handle('whatever')
- """
- #A tuple so that unbind and emit won't crash
- #bind will make it a dictionary, making it useful
- _eventBindings = ()
- def register(self, *events):
- if type(self._eventBindings) is tuple:
- self._eventBindings = {}
- for event in events:
- self._eventBindings[event] = set()
- def bind(self, event, function):
- assert event in self._eventBindings
- self._eventBindings[event].add(function)
- def unbind(self, event, function):
- assert event in self._eventBindings
- if function in self._eventBindings[event]:
- self._eventBindings[event].remove(function)
- def emit(self, event, *args):
- assert event in self._eventBindings
- for function in self._eventBindings[event]:
- function(*args)
- def __repr__(self):
- return '<EventHandler instance binding %s>' %str(self._eventBindings)
Add Comment
Please, Sign In to add comment