mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-02-18 22:51:01 +01:00
Converted schedule over to heapq from bisect.
This commit is contained in:
parent
f70919f1a8
commit
55384617d4
@ -32,21 +32,33 @@
|
|||||||
from fix import *
|
from fix import *
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import bisect
|
import heapq
|
||||||
|
|
||||||
import drivers
|
import drivers
|
||||||
|
|
||||||
class RTuple(tuple):
|
## class RTuple(tuple):
|
||||||
def __lt__(self, other):
|
## def __lt__(self, other):
|
||||||
return not tuple.__lt__(self, other)
|
## return not tuple.__lt__(self, other)
|
||||||
def __gt__(self, other):
|
## def __gt__(self, other):
|
||||||
return not tuple.__gt__(self, other)
|
## return not tuple.__gt__(self, other)
|
||||||
def __ge__(self, other):
|
## def __ge__(self, other):
|
||||||
return not tuple.__ge__(self, other)
|
## return not tuple.__ge__(self, other)
|
||||||
def __le__(self, other):
|
## def __le__(self, other):
|
||||||
return not tuple.__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):
|
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):
|
class Schedule(drivers.IrcDriver):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -59,15 +71,16 @@ class Schedule(drivers.IrcDriver):
|
|||||||
if name is None:
|
if name is None:
|
||||||
name = self.counter
|
name = self.counter
|
||||||
self.counter += 1
|
self.counter += 1
|
||||||
elif type(name) == int:
|
elif isinstance(name, int):
|
||||||
raise ValueError, 'int names are reserved for the scheduler.'
|
raise ValueError, 'int names are reserved for the scheduler.'
|
||||||
#debug.printf('Added event to schedule: %s' % name)
|
#debug.printf('Added event to schedule: %s' % name)
|
||||||
assert name not in self.events
|
assert name not in self.events
|
||||||
self.events[name] = f
|
self.events[name] = f
|
||||||
bisect.insort(self.schedule, RTuple((t, name)))
|
heapq.heappush(self.schedule, mytuple((t, name)))
|
||||||
|
|
||||||
def removeEvent(self, name):
|
def removeEvent(self, name):
|
||||||
del self.events[name]
|
del self.events[name]
|
||||||
|
heapq.heappop(self.schedule)
|
||||||
self.schedule = [(t, n) for (t, n) in self.schedule if n != name]
|
self.schedule = [(t, n) for (t, n) in self.schedule if n != name]
|
||||||
|
|
||||||
def addPeriodicEvent(self, f, t, name=None):
|
def addPeriodicEvent(self, f, t, name=None):
|
||||||
@ -82,8 +95,8 @@ class Schedule(drivers.IrcDriver):
|
|||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
#debug.printf(`(time.time(), self.schedule)`)
|
#debug.printf(`(time.time(), self.schedule)`)
|
||||||
while self.schedule and self.schedule[-1][0] < time.time():
|
while self.schedule and self.schedule[0][0] < time.time():
|
||||||
(t, name) = self.schedule.pop()
|
(t, name) = heapq.heappop(self.schedule)
|
||||||
self.events[name]()
|
self.events[name]()
|
||||||
del self.events[name]
|
del self.events[name]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user