Tree¶
commons.datastructures.tree
¶
Classes:
| Name | Description |
|---|---|
Node |
|
Functions:
| Name | Description |
|---|---|
create_node |
Create a new tree node. |
render_tree |
Yields string components to render the node as a tree. |
dumps_tree |
Return tree as a string. |
print_tree |
Print the tree. |
dumps_breadcrumbs |
Return breadcrumb paths for each node in the tree as a string. |
print_breadcrumbs |
Print breadcrumb paths for each node in the tree. |
create_node
¶
create_node(
data: T,
label: str | Callable[[T], str] = str,
parent: Node[T] | None = None,
) -> Node[T]
Create a new tree node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
T
|
The data to store in the node |
required |
label
|
str | Callable[[T], str]
|
Either a string label or a callable that extracts a label from the data |
str
|
parent
|
Node[T] | None
|
Optional parent node to attach this node to |
None
|
Returns:
| Type | Description |
|---|---|
Node[T]
|
The newly created node |
Source code in src/commons/datastructures/tree.py
render_tree
¶
render_tree(
node: Node[T],
) -> Iterator[tuple[str, str, str, T, list[T]]]
Yields string components to render the node as a tree.
The iterator returns a tuple with the following elements:
* str - The indent (usually spaces/ indent when printing the item)
* str - The prefix (e.g. ├── or └──) helpful to print directory like structures
* str - The label of the node
* T - The actual data of this item
* list[T] - All parent elements of the node, excluding the element itself. First element is root.
Source code in src/commons/datastructures/tree.py
dumps_breadcrumbs
¶
dumps_breadcrumbs(
node: Node[T],
min_depth: int = 0,
max_depth: int | None = None,
separator: str = " > ",
) -> str
Return breadcrumb paths for each node in the tree as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node[T]
|
The root node of the tree |
required |
min_depth
|
int
|
Minimum depth to include (0 = root). Nodes shallower than this are skipped. |
0
|
max_depth
|
int | None
|
Maximum depth to include (None = no limit). Nodes deeper than this are skipped. |
None
|
separator
|
str
|
The separator between path elements (default: " > ") |
' > '
|
Returns:
| Type | Description |
|---|---|
str
|
A string with one breadcrumb path per line |
Example output
Financial Services Financial Services > Banking Financial Services > Banking > Retail Banking Financial Services > Insurance
Source code in src/commons/datastructures/tree.py
print_breadcrumbs
¶
print_breadcrumbs(
node: Node[T],
min_depth: int = 0,
max_depth: int | None = None,
separator: str = " > ",
) -> None
Print breadcrumb paths for each node in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Node[T]
|
The root node of the tree |
required |
min_depth
|
int
|
Minimum depth to print (0 = root). Nodes shallower than this are skipped. |
0
|
max_depth
|
int | None
|
Maximum depth to print (None = no limit). Nodes deeper than this are skipped. |
None
|
separator
|
str
|
The separator between path elements (default: " > ") |
' > '
|