[Python-Dev] Priority queue (binary heap) python code

Fredrik Lundh fredrik@pythonware.com
Tue, 25 Jun 2002 17:33:55 +0200


Gustavo Niemeyer wrote:

> If priority queues were to be included, I'd rather add the necessary
> support in Queue to easily attach priority handling, if that's not
> already possible.

it takes a whopping four lines of code, if you're a pragmatic
programmer:

#
# implementation

import Queue, bisect

class PriorityQueue(Queue.Queue):
    def _put(self, item):
        bisect.insort(self.queue, item)

#
# usage

queue = PriorityQueue(0)

queue.put((2, "second"))
queue.put((1, "first"))
queue.put((3, "third"))

priority, value = queue.get()

</F>