A list-like implementation of heap/PriorityQueue
Project description
heap_class
list-like implementation of heap/PriorityQueue in Python
Installation
% pip install heap-class
Note the hyphen in the package name
Usage
From heap_class (underscore, not hyphen) import Heap.
Create an easy max-heap.
>>> from heap_class import Heap
>>> h = Heap([3, 1, 9, 20], max=True)
Standard list and heap methods are all available
>>> h.pop()
20
>>> h.peek() # same as h[0]
9
>>> h.push(17) # or h.append(17)
>>> h[0] # same as h.peek()
17
>>> h[1] # inefficient, but works
9
Calling reversed() turns it into a min-heap
>>> y = reversed(h)
>>> y.peek()
1
The __repr__ of a heap is inefficient, but
useful for debugging. It shows the sorted version
of the heap.
>>> y
Heap([1, 3, 9, 17], max=False)
Contains checking takes place in list's normal O(n) time.
>>> 9 in y
True
To view the raw structure of the heap, call .raw().
(this is the same view as a heapified list)
>>> y.raw()
[1, 3, 17, 9]
>>> set(y.raw()) # raw() is helpful for fast casting
{1, 3, 17, 9}
Complex entries such as tuples are supported:
>>> h2 = Heap([(6,4), (6,9), (10,2)], max=True)
>>> h2.pop()
(10, 2)
>>> h2.pop()
(6, 9)
Max heaps with tuples of different types are rather hard in heapq because of the different forms of negation each position needs. But they are easy here. This is one of the main implementation advantages of using heap-class.
>>> h2 = Heap([('aa', 4), ('aa', 5), ('zz', 2), ('zz', 1)], max=True)
>>> h2.pop()
('zz', 2)
Custom sort keys are supported. Here we will sort the heap of names by the second entry (last name)
>>> vals = [('Adam', 'Smith'), ('Zeta', 'Jones')]
>>> h3 = Heap(vals, key=lambda name: name[1])
>>> h3.peek() # Jones comes before Smith
('Zeta', 'Jones')
>>> h3.push(('Aaron', 'Allen'))
>>> h3.peek()
('Aaron', 'Allen')
replace(val) returns the current first item and
replaces it with the new item (same as
heapreplace/heapreplace_max in heapq).
Notice that if you replace the top item with a new item the order of the heap automatically changes, so the new top might differ.
>>> h3.replace(('Annie', 'Sun'))
('Aaron', 'Allen')
>>> h3.peek()
('Zeta', 'Jones')
(pushpop() is also supported, which adds the new item first
before popping the top of the heap).
It is possible to iterate further through the heap as a sorted container, though this is inefficient (useful for testing, however)
>>> for ordered_name in h3:
... print(ordered_name)
('Zeta', 'Jones')
('Adam', 'Smith')
('Annie', 'Sun')
Note that if you plan to iterate through the whole Heap, sorting with the same key has the same algorithmic efficiency but is much faster since it is highly optimized in C:
>>> for ordered_name in sorted(h3.raw(), key=lambda name: name[1]):
... print(ordered_name)
('Zeta', 'Jones')
('Adam', 'Smith')
('Annie', 'Sun')
Change Log
-
0.9.1b2 -- (2025-08)
- Add
keysupport - Added typing
- Add tests
- Removed support for
*argscreation: together withkeysupport it made typing a complete nightmare. - Remove undocumented support for
.tup=True/Falsea kludge unnecessary after*argscreation was removed. - Remove undocumented
no_private_imports.pyversion of Heap. We'll update this when heapq changes. - Point GitHub link to the right place.
- Add
-
0.9.0b1 -- (2022)
- Initial release
Credit
Copyright © 2022-25 Michael Scott Asato Cuthbert
License
MIT
Development/Distribution
- Install build tools
% pip install -U build twine pytest
- Run tests
% pytest tests/tests.py
If you get big failures, make sure you've run pip install -e . in the main directory
(with pyproject.toml in it) to make heap_class visible to the tests.
Fix any problems, obviously.
- Update
__version__inheap_class/__init__.py. - Empty existing builds:
% trash dist build *.egg-info
(if you don't have trash on your OS, delete the dist build and *.egg-info files/directories
manually or in another way)
- Build the distribution
% python -m build
- Check that artifacts render on PyPI
% twine check dist/*
- Upload to PyPI
% twine upload dist/*
You will need the proper .pypirc with passwords etc. See elsewhere on-line for
docs on that.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file heap_class-0.9.1b2.tar.gz.
File metadata
- Download URL: heap_class-0.9.1b2.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1b04e0d04c05c762704c01bd36beb52951fadcce5fb6bba296423b5c8345e31
|
|
| MD5 |
00ede62de70e8e5a4d4e27bf70408950
|
|
| BLAKE2b-256 |
2c3a45fbb8a7fde77e33b8bec36622aa22f4650d151c289bc83aefc1906ef258
|
File details
Details for the file heap_class-0.9.1b2-py3-none-any.whl.
File metadata
- Download URL: heap_class-0.9.1b2-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
319ad07981d7ed9059281f96e93d15bd0ca28b7eed10d6e3ddf26b6e767091ad
|
|
| MD5 |
0daeb6d0cc8d4edd3897f03b6eedb4e0
|
|
| BLAKE2b-256 |
0153a45860cad51603e4836d77de8982f46a3ec78975a40294571cc8a1ccbb79
|