- README: Overview and implementation details
- API Reference: Core API documentation
- Rich Printer Guide: Advanced visualization options
Tree Interval provides comprehensive AST (Abstract Syntax Tree) analysis capabilities through the AstTreeBuilder
class and related utilities.
The main class for converting Python code into Tree Interval's tree structure.
AstTreeBuilder(source: Union[FrameType, str])
- build() -> Tree[str]
- Converts source code into a tree structure
- Returns: Tree representation of the AST
Each AST node contains:
type
: The AST node type (e.g., "Module", "FunctionDef", "If")fields
: Dictionary of node-specific attributes_fields
: Raw AST node fields dataast_node
: Direct access to underlying AST node Common fields include:name
: For function and class definitionsargs
: For function argumentsbody
: For statement blockstest
: For conditional statements
Common node types and their fields:
Root node representing the entire source file
{"type": "Module", "fields": {"body": "List[n]"}}
Function definition nodes
{"type": "FunctionDef", "fields": {"name": "function_name", "args": "Arguments"}}
Conditional statements
{"type": "If", "fields": {"test": "Compare", "body": "List[n]", "orelse": "List[n]"}}
Function calls
{"type": "Call", "fields": {"func": "Name", "args": "List[n]"}}
from tree_interval import AstTreeBuilder
# Parse Python code
code = """
def example(x):
if x > 0:
return x * 2
return 0
"""
builder = AstTreeBuilder(code)
ast_tree = builder.build()
ast_tree.visualize()