haive.core.common.structures.tree¶
Automatic tree structure generator for Pydantic BaseModels with Union type support.
This module provides the AutoTree class that automatically wraps any BaseModel in a tree structure, handling complex type relationships including Union types. It enables hierarchical visualization and analysis of nested BaseModel structures.
The AutoTree automatically detects fields containing BaseModels (including those in Union types) and creates child tree nodes, making it perfect for visualizing complex data structures like plans with mixed content types.
- Usage:
from pydantic import BaseModel, Field from typing import List, Union from haive.core.common.structures.tree import AutoTree
- class Step(BaseModel):
name: str duration_hours: float = 1.0
- class Plan(BaseModel):
name: str # Can contain either Steps OR other Plans items: List[Union[Step, ‘Plan’]] = Field(default_factory=list)
# Create nested structure main_plan = Plan(name=”Project Alpha”) main_plan.items.append(Step(name=”Setup”, duration_hours=2))
sub_plan = Plan(name=”Development Phase”) sub_plan.items.append(Step(name=”Code”, duration_hours=40)) main_plan.items.append(sub_plan)
# Visualize as tree tree = AutoTree(main_plan) print(tree.visualize())
Classes¶
Automatically wraps any BaseModel in a tree structure with Union type support. |
Module Contents¶
- class haive.core.common.structures.tree.AutoTree(content, parent=None, field_source=None, **kwargs)[source]¶
Bases:
pydantic.BaseModel
,Generic
[T
]Automatically wraps any BaseModel in a tree structure with Union type support.
This generic class creates a tree representation of any BaseModel by automatically detecting fields that contain other BaseModels (including those in Union types) and creating child tree nodes for them.
The tree structure enables easy navigation, visualization, and analysis of complex nested data structures. It’s particularly useful for handling self-referential or polymorphic data structures.
- content¶
The wrapped BaseModel instance.
- _children¶
List of child AutoTree nodes.
- _parent¶
Reference to parent AutoTree node (if any).
- _field_source¶
Name of the field this node came from in its parent.
- Parameters:
Examples
>>> class Step(BaseModel): ... name: str >>> class Plan(BaseModel): ... name: str ... items: List[Union[Step, 'Plan']] = [] >>> plan = Plan(name="Main", items=[Step(name="Task1")]) >>> tree = AutoTree(plan) >>> print(tree.visualize())
Init .
- Parameters:
- visualize(show_field=True, show_type=True, max_depth=None)[source]¶
Generate a visual tree representation with customizable display options.
Creates a text-based tree visualization using Unicode box-drawing characters to show the hierarchical structure. The display can be customized to include or exclude field names, type information, and can be limited to a specific depth.
- Parameters:
- Returns:
String representation of the tree structure with appropriate indentation and connectors.
- Return type:
Example
>>> tree.visualize(show_field=True, show_type=True, max_depth=2) 'MainPlan <Plan>\\n├── [items] Setup <Step>\\n└── [items] Development <Plan>'
- model_config¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].