New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

visualize-heapq

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

visualize-heapq

A package that visualizes heapq heaps

  • 0.0.3
  • PyPI
  • Socket score

Maintainers
1

Python's heapq can be confusing to beginners.

This package helps beginners better visualize heapq heaps better

Installation

pip install visualize-heapq

Quickstart

import heapq

heap = [5, 4, 3, 2, 1]

heapq.heapify(heap) # heap is now a heapq heap (using the list data type)

print(heap)         # [1, 2, 3, 5, 4]

# we now want to visualize our heap
from visualize_heapq import visualize_heapq

visualize_heapq(heap)
#   _____1___ 
#   |       | 
# __2___    3 
# |    |      
# 5    4  

Context

What is a heap?

  • a heap is a binary tree
  • a binary tree where every parent <= its children
  • more specifically, this is known as a min heap
  • max heaps are heaps where every parent >= its children, but max heaps are not relevant here

Where heapq comes in

  • heapq is a built-in Python module
  • it provides us with functionality to use heaps (min heaps)
  • these heaps can act like priority queues
import heapq 

priority_queue: list[int] = [] # heaps are stored as lists in heapq

heapq.heappush(priority_queue, 5)
heapq.heappush(priority_queue, 10)
heapq.heappush(priority_queue, 2)
heapq.heappush(priority_queue, 6)
heapq.heappush(priority_queue, 1)

print(priority_queue) # [1, 2, 5, 10, 6]

print(heapq.heappop(priority_queue)) # 1
print(heapq.heappop(priority_queue)) # 2
print(heapq.heappop(priority_queue)) # 5
print(heapq.heappop(priority_queue)) # 6
print(heapq.heappop(priority_queue)) # 10

The weird thing about heaps in heapq

  • there is no Heap object.
  • heaps are stored as normal lists in heapq
  • which can be confusing for a beginner

As such, this package aims to make things more intuitive by allowing users to print and visualize a heapq heap (which is actually a list) directly.

Documentation Link: https://docs.python.org/3/library/heapq.html

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc