This release is a pre-release and may not be stable for production use.
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.
Metadata
Release files for heap-class 0.9.1b2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| heap_class-0.9.1b2.tar.gz | 6.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| heap_class-0.9.1b2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.4 kB
Release files / heap_class-0.9.1b2.tar.gz
| Download URL | heap_class-0.9.1b2.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f1b04e0d04c05c762704c01bd36beb52951fadcce5fb6bba296423b5c8345e31
|
|
BLAKE2b-256 checksum How to use checksums |
2c3a45fbb8a7fde77e33b8bec36622aa22f4650d151c289bc83aefc1906ef258
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.0
|
Release files / heap_class-0.9.1b2-py3-none-any.whl
| Download URL | heap_class-0.9.1b2-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
319ad07981d7ed9059281f96e93d15bd0ca28b7eed10d6e3ddf26b6e767091ad
|
|
BLAKE2b-256 checksum How to use checksums |
0153a45860cad51603e4836d77de8982f46a3ec78975a40294571cc8a1ccbb79
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.0
|
Security for heap-class 0.9.1b2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.