From f33dc0f98c224d8c93d0a37af701a9d406b6c439 Mon Sep 17 00:00:00 2001 From: dwasyl <2297074+dwasyl@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:40:46 -0600 Subject: [PATCH] Add heap priority queue when generating list of Event Occurrences This deals with possible Occurrence overlap issues when multiple Occurrences can exist (i.e. one being cancelled and another replacing it) --- schedule/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/schedule/utils.py b/schedule/utils.py index ce863887..aa42df5e 100644 --- a/schedule/utils.py +++ b/schedule/utils.py @@ -42,18 +42,21 @@ def occurrences_after(self, after=None): ] occurrences = [] + occ_count = 1 for generator in generators: try: - heapq.heappush(occurrences, (next(generator), generator)) + heapq.heappush(occurrences, (next(generator), occ_count, generator)) except StopIteration: pass + occ_count += 1 while occurrences: - generator = occurrences[0][1] + generator = occurrences[0][2] + occ_count = occurrences[0][1] try: next_occurrence = heapq.heapreplace( - occurrences, (next(generator), generator) + occurrences, (next(generator), occ_count, generator) )[0] except StopIteration: next_occurrence = heapq.heappop(occurrences)[0]