Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

Class-based heap queue implementation

Project description

Travis CI Build Latest PyPI Version

Heap queue (priority queue) implementation.

How to

Install

pip install heapqueue

Import and init an empty heap queue

from heapqueue import BinaryHeap

queue = BinaryHeap()

# BinaryHeap can accept two custom parameters
# comp: a function that indicates how to compare two elements
#     return -1 if a < b, 1 if a > b, and 0 if equal
# size: fixed size of heap queue to truncate results
queue = BinaryHeap(comp=lambda a, b: a['val'] - b['val'], size=3)

Insert elements

# primitive data types
queue.push(9)
queue.push('hello')

# objects
# note: `comp` is necessary if you want to push objects
queue.push({'foo': 'a', 'bar': 1})
queue.push({'foo': 'b', 'bar': 3})

Get min/max elements

# delete and return the top(max/min) one
queue.pop()

# return a sorted list according to `comp` and `size`
# note: this is a property, not a method
queue.sorted_list

# BinaryHeap can handle iteration as well
for item in queue:
    print(item)

# you can also just have a look at top one without deleting it
queue.peek()

Remove elements

# remove item equals to 12
queue.remove(12)

# remove item using `comp` for objects
queue.remove({'foo': 'a', 'bar': 1})

License

Code and documentation are available according to the MIT License (see LICENSE file).

Project details


Release history Release notifications

This version
History Node

0.1b4

History Node

0.1b2

History Node

0.1b1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
heapqueue-0.1b4-py2.py3-none-any.whl (4.7 kB) Copy SHA256 hash SHA256 Wheel py2.py3 Feb 23, 2017
heapqueue-0.1b4.tar.gz (5.5 kB) Copy SHA256 hash SHA256 Source None Feb 23, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page