Message299929
I don't see any way to change PriorityQueue to fix this. The non-comparability of dicts likely won't be restored, nor will the lexicographic comparison order of tuples be likely to change.
One possible way forward is to provide a wrapper with the desired comparison behavior:
pq = PriorityQueue()
pq.put(Prioritize(13, task))
task = pq.get().item
where Prioritize is implemented something like this:
import functools
@functools.total_ordering
class Prioritize:
def __init__(self, priority, item):
self.priority = priority
self.item = item
def __eq__(self, other):
return self.priority == other.priority
def __lt__(self, other):
return self.priority < other.priority
from queue import PriorityQueue, Empty
from contextlib import suppress
bugged = [
(-25.691, {'feedback': 13, 'sentiment': 0.309, 'support_ticket': 5}), (-25.691, {'feedback': 11, 'sentiment': 0.309, 'support_ticket': 3}), (-25.0, {'feedback': 23, 'sentiment': 0.0, 'support_ticket': 15}),
]
pq = PriorityQueue()
for priority, item in bugged:
pq.put(Prioritize(priority, item))
with suppress(Empty):
while True:
item = pq.get_nowait().item
print(item) |
|
Date |
User |
Action |
Args |
2017-08-08 14:51:06 | rhettinger | set | recipients:
+ rhettinger, Mikołaj Babiak |
2017-08-08 14:51:06 | rhettinger | set | messageid: <1502203866.23.0.705408147073.issue31145@psf.upfronthosting.co.za> |
2017-08-08 14:51:06 | rhettinger | link | issue31145 messages |
2017-08-08 14:51:05 | rhettinger | create | |
|