Converted schedule over to heapq from bisect.

This commit is contained in:
Jeremy Fincher 2003-08-29 14:18:18 +00:00
parent f70919f1a8
commit 55384617d4

View File

@ -32,21 +32,33 @@
from fix import *
import time
import bisect
import heapq
import drivers
class RTuple(tuple):
def __lt__(self, other):
return not tuple.__lt__(self, other)
def __gt__(self, other):
return not tuple.__gt__(self, other)
def __ge__(self, other):
return not tuple.__ge__(self, other)
def __le__(self, other):
return not tuple.__le__(self, other)
## class RTuple(tuple):
## def __lt__(self, other):
## return not tuple.__lt__(self, other)
## def __gt__(self, other):
## return not tuple.__gt__(self, other)
## def __ge__(self, other):
## return not tuple.__ge__(self, other)
## def __le__(self, other):
## return not tuple.__le__(self, other)
## def __cmp__(self, other):
## return -1*tuple.__cmp__(self, other)
class mytuple(tuple):
def __cmp__(self, other):
return -1*tuple.__cmp__(self, other)
return cmp(self[0], other[0])
def __le__(self, other):
return self[0] <= other[0]
def __lt__(self, other):
return self[0] < other[0]
def __gt__(self, other):
return self[0] > other[0]
def __ge__(self, other):
return self[0] >= other[0]
class Schedule(drivers.IrcDriver):
def __init__(self):
@ -59,15 +71,16 @@ class Schedule(drivers.IrcDriver):
if name is None:
name = self.counter
self.counter += 1
elif type(name) == int:
elif isinstance(name, int):
raise ValueError, 'int names are reserved for the scheduler.'
#debug.printf('Added event to schedule: %s' % name)
assert name not in self.events
self.events[name] = f
bisect.insort(self.schedule, RTuple((t, name)))
heapq.heappush(self.schedule, mytuple((t, name)))
def removeEvent(self, name):
del self.events[name]
heapq.heappop(self.schedule)
self.schedule = [(t, n) for (t, n) in self.schedule if n != name]
def addPeriodicEvent(self, f, t, name=None):
@ -82,8 +95,8 @@ class Schedule(drivers.IrcDriver):
def run(self):
#debug.printf(`(time.time(), self.schedule)`)
while self.schedule and self.schedule[-1][0] < time.time():
(t, name) = self.schedule.pop()
while self.schedule and self.schedule[0][0] < time.time():
(t, name) = heapq.heappop(self.schedule)
self.events[name]()
del self.events[name]