This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients Mikołaj Babiak, rhettinger
Date 2017-08-08.14:51:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1502203866.23.0.705408147073.issue31145@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2017-08-08 14:51:06rhettingersetrecipients: + rhettinger, Mikołaj Babiak
2017-08-08 14:51:06rhettingersetmessageid: <1502203866.23.0.705408147073.issue31145@psf.upfronthosting.co.za>
2017-08-08 14:51:06rhettingerlinkissue31145 messages
2017-08-08 14:51:05rhettingercreate