Raymond Hettinger added the comment:

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 

    pq = PriorityQueue()
    pq.put(Prioritize(13, task))
    task = pq.get().item
where Prioritize is implemented something like this:

import functools

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

assignee:  -> rhettinger
nosy: +rhettinger
type: behavior -> enhancement
versions: +Python 3.7 -Python 3.6

Python tracker <rep...@bugs.python.org>
Python-bugs-list mailing list

Reply via email to