-
Notifications
You must be signed in to change notification settings - Fork 0
Home
kairos edited this page Jan 8, 2025
·
11 revisions
A powerful Python package for managing, analyzing and visualizing tree structures with rich interval-based node positioning.
- Position-Aware Nodes: Track code positions with line numbers, column offsets and intervals
- AST Analysis: Built-in support for Python AST traversal and node location
- Frame Analysis: Runtime code inspection with frame position tracking
- Rich Visualization: Multiple visualization options including ASCII trees and Rich-based pretty printing
- JSON Serialization: Full support for saving and loading tree structures
- Flexible Node Search: Parent, child and sibling search with custom predicates
Tree Interval provides tools for:
- Managing tree structures with position tracking
- AST (Abstract Syntax Tree) analysis
- Frame analysis for runtime code inspection
- Position-aware node tracking
- Customizable tree visualization
- JSON serialization/deserialization
from tree_interval import Tree, Leaf, Position
# Create a basic tree
tree = Tree("Example")
root = Leaf(Position(0, 100, "Root"))
child = Leaf(Position(10, 50, "Child"))
tree.root = root
tree.add_leaf(child)
# Visualize the tree
tree.visualize()
- For core components like Tree, Leaf, and Position, see Core Components
- For visualization options, check the Visualization Guide
- For detailed API documentation, refer to the API Reference
- Basic Position: Simple start/end interval
pos = Position(0, 100, "Basic")
- Line-Aware Position: Track line numbers
pos = Position(0, 100, "Lines")
pos.lineno = 1
pos.end_lineno = 5
- Column-Aware Position: Full position tracking
pos = Position(0, 100, "Columns")
pos.col_offset = 4
pos.end_col_offset = 8
- Parent Search:
# Find parent node matching predicate
node.find_parent(lambda n: n.info.get("type") == "FunctionDef")
- Child Search:
# Find first child matching predicate
node.find_child(lambda n: n.info.get("name") == "my_var")
- Sibling Search:
# Find sibling node
node.find_sibling(lambda n: n.info.get("type") == "ClassDef")
- Basic ASCII Tree:
tree.visualize()
- Rich Pretty Printing:
from tree_interval.rich_printer import RichTreePrinter
printer = RichTreePrinter()
printer.print_tree(tree)
-
ast
: Python's built-in AST module (Tree Interval adds position tracking) -
asttokens
: Token-based AST analysis (Tree Interval provides richer interval model) -
astroid
: Python AST framework (Tree Interval focuses on general tree structures)
-
Code Analysis
- Track source positions in AST nodes
- Locate runtime code execution points
- Analyze code structure and relationships
-
Tree Visualization
- Debug tree structures
- Generate documentation
- Analyze hierarchical data
-
Position Tracking
- Map source locations
- Track text positions
- Handle nested intervals