+class Nerver():
+ # A little utility class used to run the nervlists. A nervlist is a list of (n, f) tuples where f() is run after n seconds.
+ # If f returns something, that's also returned by nerv.
+ def __init__(self, nervlist):
+ self.nervlist = list(nervlist)
+ self.last_event_time = time.time()
+
+ def nerv(self):
+ if len(self.nervlist):
+ (time, f) = self.nervlist[0]
+ now = time.time()
+ time_gone = now-self.last_event_time
+ # check if the first element is to be triggered
+ if time_gone >= time:
+ self.nervlist = self.nervlist[1:] # "pop" the first element, but do not modify original list
+ self.last_event_time = now
+ return f()
+
+