Skip to main content

pytreeds

A dependency-free, beginner-focused Python tree library suitable for Pyodide. One Tree variable holds all nodes, and navigation/traversal uses names (strings) consistently. Attach optional Python data to individual nodes.

Version: The current source release is 0.2.3. See the PyPI project page for the latest published version.

Quick start

from pytreeds import Tree

org = Tree()
org.add_node("CEO", data={"level": 1})
org.add_node("CTO", parent="CEO", data=[90, 85])
org.add_node("CFO", parent="CEO")
org.add_node("COO", parent="CEO")
org.add_node("Engineering", parent="CTO")
org.add_node("DevOps", parent="CTO")
print(org)

print(org.get_node_data("CTO"))  # [90, 85]
org.set_node_data("CTO", {"team": "Technology"})
print(org.get_node_data("CTO"))

print(list(org.dfs()))
print(list(org.bfs()))

org.move_node("CTO", parent="COO")  # The entire CTO subtree moves
org.delete_node("CFO")             # Works on leaf or subtree
print(org)

# Three layouts for the same tree
print(org.get_diagram(style="branch"))
print(org.get_diagram(style="horizontal"))
print(org.get_diagram(style="vertical"))

# Include node data or display a smaller subtree
print(org.get_diagram("CTO", max_depth=1, show_data=True))
# Data is shown beneath each name, using double quotes for strings

Omitting parent in add_node() creates a root only when the tree is empty. Every later node requires a parent. One Tree contains exactly one root.

Install

Regular Python: pip install pytreeds

Pyodide (asynchronous package loader):

import micropip
await micropip.install("pytreeds")

API summary

Build: Tree(), add_node(name, parent=None, data=None).

Edit: delete_node(), move_node(), rename_node(), copy(). Removing a node removes all its descendants; moving one moves its entire subtree.

Inspect: get_root(), get_size(), get_height(), get_depth(), get_leaf_count(), is_leaf(), contains().

Relationships: get_parent(), get_children(), get_siblings(), get_ancestors(), get_descendants(), get_leaves().

Data: get_node_data(name), set_node_data(name, data). Any Python object can be attached, including lists and dictionaries. Values are returned by reference; copy() deep-copies data, so objects incompatible with deepcopy() may cause an error when copying.

Traversal: dfs(), bfs(), and ordinary for name in tree. The dfs() method uses left-to-right preorder (visits parents before their children).

Diagrams: print(tree) for display, or get_diagram(name=None, style='branch', max_depth=None, show_data=False) to retrieve a diagram as a string.

See the illustrated terminology guide and the Detailed Reference, with a dedicated page for every method.

Local tests

python -m unittest discover -s tests -v

Releases are gated by automated unit tests and a strict Sphinx documentation build.

Metadata

Release files for pytreeds 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytreeds 0.2.3
File Size Uploaded Provenance Actions
pytreeds-0.2.3.tar.gz 12.4 kB Without provenance Details for pytreeds-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytreeds 0.2.3
File Interpreter ABI Platform Actions
pytreeds-0.2.3-py3-none-any.whl Python 3 none any Details for pytreeds-0.2.3-py3-none-any.whl

Total release size: 20.8 kB

Release files / pytreeds-0.2.3.tar.gz

Download URL pytreeds-0.2.3.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d490d11cda45563f16c0e4612c48ca557b954279b2550cae873e0fdf1b9f0969
BLAKE2b-256 checksum
How to use checksums
cf639532d76ef6fb8e799caea3f026a1a26cd0bc38c9c4779caa263de9bb0c0f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / pytreeds-0.2.3-py3-none-any.whl

Download URL pytreeds-0.2.3-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2beaf25023ab2291ba94a656079ba339b44d8ede1c5a1ea05e39ce027306846a
BLAKE2b-256 checksum
How to use checksums
fcb7c36a72f794d63cd2cc128c7f4ea483954492c176ef7942b56d6df9c0b65a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.16

Security for pytreeds 0.2.3

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page