This is a toy example of a waitlist (`PriorityQueue') in which each surgery on the waitlist should have a lexicographical order on the pairs (p, date). The p is an integer and the date is a datetime object.
Clearly integers in python have an order, but so do these datetime objects. And I had thought that Is there a Lexicographic PriorityQueue in Python's standard library? had taught me that I just need to implement __lt__
for my Surgery
object.
But the following minimal working example shows that the order of the surgeries on the waitlist is wrong.
from queue import PriorityQueue as PQ
import numpy as np
import pandas as pd
np.random.seed(123)
waitlist = PQ()
class Surgery:
def __init__(self, a, b):
self.priority = (a,b)
def __lt__(self, other):
return self.priority < other.priority
def __repr__(self):
return f'Surgery({self.priority})'
# Some fake data
x = np.random.randint(1,3,size=10)
y = pd.date_range('2022-01-01', '2022-01-10')
# Instantiate objects and put them on queue
for i,j in zip(x,y):
waitlist.put(Surgery(i,j))
for s in waitlist.queue:
print(s)
Which outputs:
Surgery((1, Timestamp('2022-01-01 00:00:00', freq='D')))
Surgery((1, Timestamp('2022-01-04 00:00:00', freq='D')))
Surgery((1, Timestamp('2022-01-03 00:00:00', freq='D')))
Surgery((2, Timestamp('2022-01-02 00:00:00', freq='D')))
Surgery((1, Timestamp('2022-01-05 00:00:00', freq='D')))
Surgery((1, Timestamp('2022-01-06 00:00:00', freq='D')))
Surgery((1, Timestamp('2022-01-07 00:00:00', freq='D')))
Surgery((2, Timestamp('2022-01-08 00:00:00', freq='D')))
Surgery((2, Timestamp('2022-01-09 00:00:00', freq='D')))
Surgery((1, Timestamp('2022-01-10 00:00:00', freq='D')))
Printing the surgeries in order of the queue shows that the relative position of the p
is not satisfied. I don't understand why this is the case. One guess is that the PriorityQueue
is not actually satisfying the order, or that the order of elements in waitlist.queue
is not the true order represented by the underlying heap.
What is going on with the apparent queue order (and how do I fix it)?