Title: Priority Queues
Slug: priority_queues
Summary: Priority Queues Using Python.
Date: 2017-02-02 12:00
Category: Python
Tags: Basics
Authors: Chris Albon
In [1]:
import heapq
In [2]:
# Create a priority queue abstract base class
class priority_queue:
# Initialize the instance
def __init__(self):
# Create a list to use as the queue
self._queue = []
# Create an index to use as ordering
self._index = 0
# Create a function to add a task to the queue
def add_task(self, item, priority):
# Push the arguments to the _queue using a heap
heapq.heappush(self._queue, (-priority, self._index, item))
# Add one to the index
self._index += 1
# Create a function to get the next item from the queue
def next_task(self):
# Return the next item in the queue
return heapq.heappop(self._queue)[-1]
In [3]:
# Create a priority queue called task_list
task_list = priority_queue()
In [4]:
# Add an item to the queue
task_list.add_task('Clean Dishes', 1)
# Add an item to the queue
task_list.add_task('Wash Car', 2)
# Add an item to the queue
task_list.add_task('Walk Dog', 3)
In [5]:
# Retrieve items from the queue
task_list.next_task()
Out[5]:
In [6]:
# Retrieve items from the queue
task_list.next_task()
Out[6]:
In [7]:
# Retrieve items from the queue
task_list.next_task()
Out[7]: