Skip to content

Commit 330c408

Browse files
committed
Changed: hprint tree to allow hiding names of intermediate nodes
1 parent 63768cd commit 330c408

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

bigtree/tree/export.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ def hprint_tree(
377377
tree: T,
378378
node_name_or_path: str = "",
379379
max_depth: int = 0,
380+
intermediate_node_name: bool = True,
380381
style: str = "const",
381382
custom_style: Iterable[str] = [],
382383
) -> None:
@@ -457,12 +458,14 @@ def hprint_tree(
457458
tree (Node): tree to print
458459
node_name_or_path (str): node to print from, becomes the root node of printing
459460
max_depth (int): maximum depth of tree to print, based on `depth` attribute, optional
461+
intermediate_node_name (bool): indicator if intermediate nodes have node names, defaults to True
460462
style (str): style of print, defaults to const style
461463
custom_style (Iterable[str]): style of icons, used when `style` is set to 'custom'
462464
"""
463465
result = hyield_tree(
464466
tree,
465467
node_name_or_path=node_name_or_path,
468+
intermediate_node_name=intermediate_node_name,
466469
max_depth=max_depth,
467470
style=style,
468471
custom_style=custom_style,
@@ -474,6 +477,7 @@ def hyield_tree(
474477
tree: T,
475478
node_name_or_path: str = "",
476479
max_depth: int = 0,
480+
intermediate_node_name: bool = True,
477481
style: str = "const",
478482
custom_style: Iterable[str] = [],
479483
) -> List[str]:
@@ -555,6 +559,7 @@ def hyield_tree(
555559
tree (Node): tree to print
556560
node_name_or_path (str): node to print from, becomes the root node of printing
557561
max_depth (int): maximum depth of tree to print, based on `depth` attribute, optional
562+
intermediate_node_name (bool): indicator if intermediate nodes have node names, defaults to True
558563
style (str): style of print, defaults to const style
559564
custom_style (Iterable[str]): style of icons, used when `style` is set to 'custom'
560565
@@ -609,9 +614,10 @@ def hyield_tree(
609614

610615
# Calculate padding
611616
space = " "
612-
padding_depths = {}
613-
for _idx, _children in enumerate(levelordergroup_iter(tree)):
614-
padding_depths[_idx + 1] = max([len(node.node_name) for node in _children])
617+
padding_depths = collections.defaultdict(int)
618+
if intermediate_node_name:
619+
for _idx, _children in enumerate(levelordergroup_iter(tree)):
620+
padding_depths[_idx + 1] = max([len(node.node_name) for node in _children])
615621

616622
def _hprint_branch(_node: T) -> Tuple[List[str], int]:
617623
"""Get string for tree horizontally.
@@ -623,17 +629,19 @@ def _hprint_branch(_node: T) -> Tuple[List[str], int]:
623629
Returns:
624630
(Tuple[List[str], int]): Intermediate/final result for node, index of branch
625631
"""
626-
padding_depth = padding_depths[_node.depth]
627-
padding = space * (padding_depth + 4)
628-
node_name_centered = _node.node_name.center(padding_depth)
632+
node_name_centered = _node.node_name.center(padding_depths[_node.depth])
629633

630634
children = list(_node.children)
631635
if not len(children):
632636
node_str = f"{style_branch} {node_name_centered.rstrip()}"
633637
return [node_str], 0
634638

635639
result, result_nrow, result_idx = [], [], []
636-
node_str = f"""{style_branch} {node_name_centered} {style_branch}"""
640+
if intermediate_node_name:
641+
node_str = f"""{style_branch} {node_name_centered} {style_branch}"""
642+
else:
643+
node_str = f"""{style_branch}{style_branch}{style_branch}"""
644+
padding = space * len(node_str)
637645
for idx, child in enumerate(children):
638646
result_child, result_branch_idx = _hprint_branch(child)
639647
result.extend(result_child)

tests/tree/test_export.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,30 @@ def test_hprint_tree_child_node_path(tree_node):
316316
node_name_or_path="a/b",
317317
)
318318

319+
@staticmethod
320+
def test_hprint_tree_intermediate_node_name(tree_node):
321+
expected_str = (
322+
" ┌─ d\n ┌───┤ ┌─ g\n───┤ └───┤\n │ └─ h\n └───── f\n"
323+
)
324+
assert_print_statement(
325+
hprint_tree,
326+
expected_str,
327+
tree=tree_node,
328+
intermediate_node_name=False,
329+
)
330+
331+
@staticmethod
332+
def test_hprint_tree_intermediate_node_name_diff_node_name_length(tree_node):
333+
tree_node["b"].name = "bcde"
334+
tree_node["c"]["f"].name = "fghijk"
335+
expected_str = " ┌─ d\n ┌───┤ ┌─ g\n───┤ └───┤\n │ └─ h\n └───── fghijk\n"
336+
assert_print_statement(
337+
hprint_tree,
338+
expected_str,
339+
tree=tree_node,
340+
intermediate_node_name=False,
341+
)
342+
319343
@staticmethod
320344
def test_hprint_tree_style_ansi(tree_node):
321345
expected_str = " /- d\n /- b -+ /- g\n- a -+ \\- e -+\n | \\- h\n \\- c --- f\n"

0 commit comments

Comments
 (0)