Skip to content

Repository files navigation

logo nutree

Latest Version Tests codecov License Documentation Status Downloads

Nutree lets you organize and query arbitrary Python objects as a tree — with clones, diffing, and graph export built in.

Trees and nodes behave like familiar Python containers wherever that makes sense — len(tree) counts nodes, for node in tree: ... walks the tree depth-first, and value in tree tests membership. Lookup (tree[value]) matches by id or by the wrapped data (not by position, since a tree has no natural order).

Tip

Take the Tour and Read the Docs for more.

Nutree Facts

Handle multiple references of single objects ('clones')
Search by name pattern, id, or object reference
Compare two trees and calculate patches
Unobtrusive handling of arbitrary objects
Save as DOT file and graphwiz diagram
Nodes can be plain strings or objects
(De)Serialize to (compressed) JSON
Save as Mermaid flow diagram
Multiple traversal methods
Generate random trees
Convert to RDF graph
Fully type annotated
Typed child nodes
Memory efficient
Pretty print
Navigation
Filtering
Fast

Example

A simple tree, with text nodes

from nutree import Tree, Node

tree = Tree("Store")

n = tree.add("Records")

n.add("Let It Be")
n.add("Get Yer Ya-Ya's Out!")

n = tree.add("Books")
n.add("The Little Prince")

tree.print()
Tree<'Store'>
├─── 'Records'
│    ├─── 'Let It Be'
│    ╰─── "Get Yer Ya-Ya's Out!"
╰─── 'Books'
     ╰─── 'The Little Prince'

Tree nodes wrap the data and also expose methods for navigation, searching, iteration, ...

records_node = tree["Records"]
assert isinstance(records_node, Node)
assert records_node.name == "Records"

print(records_node.first_child())
Node<'Let It Be', data_id=510268653885439170>

Nodes can holdarbitrary objects (not just strings):

alice = Person("Alice", age=23, guid="{123-456}")
tree.add(alice)

# Lookup nodes by object, data_id, name pattern, ...
alice_node = tree[alice]
assert isinstance(alice_node.data, Person)

del tree[alice]

Take the Tour and Read the Docs for more.

About

A Python library that lets you organize and query arbitrary objects as a tree — with clones, diffing, and graph export built in.

Topics

Resources

Stars

56 stars

Watchers

2 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages