How do I reverse the order of PriorityQueue in python?
Question: I have created a simple priority queue in python that orders items by their
value:

    
    
    import Queue
    q = Queue.PriorityQueue()
    for it in items:
        q.put((it.value, it))
    

but when i print the queue using:

    
    
    while not q.empty()
        print q.get()
    

it will always print the lowest value first. is there a way of getting the
last item in a queue without changing the last two lines in the top bit of
code to:

    
    
    for it in items:
        q.put((-1*it.value, it))
    

because that seems a bit messy and creates problems if i want to use that
information for something else (i would have to multiply it by -1 again)


Answer: You _could_ just make your own class that inherits from PriorityQueue and does
the messy -1 multiplication under the hood for you:

    
    
    class ReversePriorityQueue(PriorityQueue):
    
    def put(self, tup):
        newtup = tup[0] * -1, tup[1]
        PriorityQueue.put(self, newtup)
    
    def get(self):
        tup = PriorityQueue.get(self)
        newtup = tup[0] * -1, tup[1]
        return newtup
    

This appears to work with tuples, at least:

    
    
    Q = ReversePriorityQueue()
    
    In [94]: Q.put((1,1))
    
    In [95]: Q.get()
    Out[95]: (1, 1)
    
    In [96]: Q.put((1,1))
    
    In [97]: Q.put((5,5))
    
    In [98]: Q.put((9,9))
    
    In [99]: Q.get()
    Out[99]: (9, 9)
    
    In [100]: Q.get()
    Out[100]: (5, 5)
    
    In [101]: Q.get()
    Out[101]: (1, 1)
    

I'm sure you could generalize the code to work with more than just tuples from
here.

