MCPcopy Create free account
hub / github.com/dabeaz/python-cookbook / PriorityQueue

Class PriorityQueue

src/1/implementing_a_priority_queue/example.py:7–17  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import heapq
6
7class PriorityQueue:
8 def __init__(self):
9 self._queue = []
10 self._index = 0
11
12 def push(self, item, priority):
13 heapq.heappush(self._queue, (-priority, self._index, item))
14 self._index += 1
15
16 def pop(self):
17 return heapq.heappop(self._queue)[-1]
18
19# Example use
20class Item:

Callers 1

example.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected