-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduler.py
More file actions
37 lines (26 loc) · 1.2 KB
/
Scheduler.py
File metadata and controls
37 lines (26 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from __future__ import annotations
from typing import List, Tuple, Optional, Callable
from time import sleep
from datetime import datetime
from ScheduledEvent import ScheduledEvent
class EventScheduler:
def __init__(self) -> None:
self._events: List[Tuple[int, ScheduledEvent]] = []
def add_event(self, event: ScheduledEvent, priority: int = 0) -> None:
self._events.append((priority, event))
def remove_event(self, event: ScheduledEvent) -> bool:
for i, (_p, e) in enumerate(self._events):
if e == event:
del self._events[i]
return True
return False
def get_matching_events(self, now: Optional[datetime] = None) -> List[Tuple[int, ScheduledEvent]]:
now = now or datetime.now()
return [(p, e) for (p, e) in self._events if e.contains(now)]
def get_highest_priority_event(self, now: Optional[datetime] = None) -> Optional[ScheduledEvent]:
matches = self.get_matching_events(now)
if not matches:
return None
# sort by priority descending, tie-breaker by earliest start_datetime
matches.sort(key=lambda pe: (-pe[0], pe[1].start_datetime))
return matches[0][1]