From ad1b36a6433adb963ce18f7de9eb2113b8245693 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Thu, 19 Aug 2004 16:58:42 +0000 Subject: [PATCH] Added rescheduleEvent. --- src/schedule.py | 10 +++++++--- test/test_schedule.py | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/schedule.py b/src/schedule.py index 65d2f201f..140399d5f 100644 --- a/src/schedule.py +++ b/src/schedule.py @@ -85,8 +85,6 @@ class Schedule(drivers.IrcDriver): if name is None: name = self.counter self.counter += 1 - elif isinstance(name, int): - raise ValueError, 'int names are reserved for the scheduler.' assert name not in self.events self.events[name] = f heapq.heappush(self.schedule, mytuple((t, name))) @@ -94,7 +92,7 @@ class Schedule(drivers.IrcDriver): def removeEvent(self, name): """Removes the event with the given name from the schedule.""" - del self.events[name] + f = self.events.pop(name) self.schedule = [(t, n) for (t, n) in self.schedule if n != name] # We must heapify here because the heap property may not be preserved # by the above list comprehension. We could, conceivably, just mark @@ -102,6 +100,11 @@ class Schedule(drivers.IrcDriver): # but that would only save a constant factor (we're already linear for # the listcomp) so I'm not worried about it right now. heapq.heapify(self.schedule) + return f + + def rescheduleEvent(self, name, t): + f = self.removeEvent(name) + self.addEvent(f, t, name=name) def addPeriodicEvent(self, f, t, name=None): """Adds a periodic event that is called every t seconds.""" @@ -134,6 +137,7 @@ except NameError: addEvent = schedule.addEvent removeEvent = schedule.removeEvent +rescheduleEvent = schedule.rescheduleEvent addPeriodicEvent = schedule.addPeriodicEvent removePeriodicEvent = removeEvent run = schedule.run diff --git a/test/test_schedule.py b/test/test_schedule.py index 201949fce..88340e00f 100644 --- a/test/test_schedule.py +++ b/test/test_schedule.py @@ -61,6 +61,19 @@ class TestSchedule(SupyTestCase): time.sleep(3) self.assertEqual(i[0], 11) + def testReschedule(self): + sched = schedule.Schedule() + i = [0] + def inc(): + i[0] += 1 + n = sched.addEvent(inc, time.time() + 1) + sched.rescheduleEvent(n, time.time() + 3) + time.sleep(1.2) + sched.run() + self.assertEqual(i[0], 0) + time.sleep(2) + sched.run() + self.assertEqual(i[0], 1) # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: