def append(self, callback): """Just like list.append, except it can also handle lists and discards None-values""" if callback is None: pass elif callable(callback): list.append(self, WeakMethod(callback)) elif isinstance(callback, list, tuple): for i in callback: self.append(i) else: assert False
class _Event(object): def __init__(self, loop, callback, timeout, starttime): self.loop = loop self.callback = WeakMethod(callback) self.timeout = timeout self._last_dt = starttime self._dt = 0. def do(self, dt): if self.callback.is_dead(): return False self.callback()(dt) def tick(self, curtime): # timeout happen ? if curtime - self._last_dt < self.timeout: return True # calculate current timediff for this event self._dt = curtime - self._last_dt self._last_dt = curtime # call the callback if self.callback.is_dead(): return False ret = self.callback()(self._dt) # if it's a once event, don't care about the result # just remove the event if not self.loop: return False # if user return an explicit false, # remove the event if ret == False: return False return True
def __init__(self, loop, callback, timeout, starttime): self.loop = loop self.callback = WeakMethod(callback) self.timeout = timeout self._last_dt = starttime self._dt = 0.
def __contains__(self, elem): if isinstance(elem, WeakMethod): return list.__contains__(self, elem) else: return WeakMethod(elem) in self
def remove(self, elem): if not isinstance(elem, WeakMethod): elem = WeakMethod(elem) list.remove(self, elem)