Skip to content

Commit c12585a

Browse files
authored
Merge pull request #176 from kayjan/fix-hprint
Fixed: hprint and hyield (and hshow) to support binary node
2 parents 97c8bff + e8de93a commit c12585a

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ repos:
1111
- id: requirements-txt-fixer
1212
- id: trailing-whitespace
1313
- repo: https://github.com/pycqa/isort
14-
rev: 5.12.0
14+
rev: 5.13.2
1515
hooks:
1616
- id: isort
1717
name: isort (python)
1818
- repo: https://github.com/psf/black
19-
rev: 22.10.0
19+
rev: 24.1.0
2020
hooks:
2121
- id: black
2222
- repo: https://github.com/PyCQA/flake8

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [0.15.7] - 2023-01-26
810
### Added
911
- Misc: Sphinx documentation to support mermaid markdown images, reflect CHANGELOG section, add more emojis.
1012
### Changed
1113
- Misc: Update SEO image.
1214
- Misc: Fix Sphinx documentation font size difference between web and mobile, include last updated date.
15+
- Misc: Upgrade package versions in pre-commit hook.
16+
### Fixed
17+
- Tree Exporter: `hprint_tree` and `hyield_tree` to be compatible with `BinaryNode` where child nodes can be None type.
1318

1419
## [0.15.6] - 2023-01-20
1520
### Added
@@ -468,7 +473,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
468473
- Utility Iterator: Tree traversal methods.
469474
- Workflow To Do App: Tree use case with to-do list implementation.
470475

471-
[Unreleased]: https://github.com/kayjan/bigtree/compare/0.15.6...HEAD
476+
[Unreleased]: https://github.com/kayjan/bigtree/compare/0.15.7...HEAD
477+
[0.15.7]: https://github.com/kayjan/bigtree/compare/0.15.6...0.15.7
472478
[0.15.6]: https://github.com/kayjan/bigtree/compare/0.15.5...0.15.6
473479
[0.15.5]: https://github.com/kayjan/bigtree/compare/0.15.4...0.15.5
474480
[0.15.4]: https://github.com/kayjan/bigtree/compare/0.15.3...0.15.4

bigtree/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.15.6"
1+
__version__ = "0.15.7"
22

33
from bigtree.binarytree.construct import list_to_binarytree
44
from bigtree.dag.construct import dataframe_to_dag, dict_to_dag, list_to_dag
@@ -75,4 +75,4 @@
7575
from bigtree.workflows.app_calendar import Calendar
7676
from bigtree.workflows.app_todo import AppToDo
7777

78-
sphinx_versions = ["latest", "0.15.6", "0.14.8"]
78+
sphinx_versions = ["latest", "0.15.7", "0.14.8"]

bigtree/tree/construct.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,9 +1068,11 @@ def _create_node(
10681068
elif _cumulative_string:
10691069
_new_node.set_attrs(
10701070
{
1071-
length_attr: int(_cumulative_string)
1072-
if _cumulative_string.isdigit()
1073-
else float(_cumulative_string)
1071+
length_attr: (
1072+
int(_cumulative_string)
1073+
if _cumulative_string.isdigit()
1074+
else float(_cumulative_string)
1075+
)
10741076
}
10751077
)
10761078

bigtree/tree/export.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,19 +619,22 @@ def hyield_tree(
619619
for _idx, _children in enumerate(levelordergroup_iter(tree)):
620620
padding_depths[_idx + 1] = max([len(node.node_name) for node in _children])
621621

622-
def _hprint_branch(_node: T) -> Tuple[List[str], int]:
622+
def _hprint_branch(_node: Union[T, Node], _cur_depth: int) -> Tuple[List[str], int]:
623623
"""Get string for tree horizontally.
624624
Recursively iterate the nodes in post-order traversal manner.
625625
626626
Args:
627627
_node (Node): node to get string
628+
_cur_depth (int): current depth of node
628629
629630
Returns:
630631
(Tuple[List[str], int]): Intermediate/final result for node, index of branch
631632
"""
632-
node_name_centered = _node.node_name.center(padding_depths[_node.depth])
633+
if not _node:
634+
_node = Node(" ")
635+
node_name_centered = _node.node_name.center(padding_depths[_cur_depth])
633636

634-
children = list(_node.children)
637+
children = list(_node.children) if any(list(_node.children)) else []
635638
if not len(children):
636639
node_str = f"{style_branch} {node_name_centered.rstrip()}"
637640
return [node_str], 0
@@ -643,7 +646,7 @@ def _hprint_branch(_node: T) -> Tuple[List[str], int]:
643646
node_str = f"""{style_branch}{style_branch}{style_branch}"""
644647
padding = space * len(node_str)
645648
for idx, child in enumerate(children):
646-
result_child, result_branch_idx = _hprint_branch(child)
649+
result_child, result_branch_idx = _hprint_branch(child, _cur_depth + 1)
647650
result.extend(result_child)
648651
result_nrow.append(len(result_child))
649652
result_idx.append(result_branch_idx)
@@ -712,7 +715,7 @@ def _hprint_branch(_node: T) -> Tuple[List[str], int]:
712715
result = [prefix + stem for prefix, stem in zip(result_prefix, result)]
713716
return result, mid
714717

715-
result, _ = _hprint_branch(tree)
718+
result, _ = _hprint_branch(tree, 1)
716719
return result
717720

718721

tests/node/test_binarynode.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from bigtree.node.basenode import BaseNode
66
from bigtree.node.binarynode import BinaryNode
77
from bigtree.node.node import Node
8+
from bigtree.tree.export import hprint_tree
89
from bigtree.tree.helper import clone_tree
910
from bigtree.utils.exceptions import LoopError, TreeError
1011
from tests.conftest import assert_print_statement
@@ -970,6 +971,21 @@ def assert_binarytree_structure_root2(root):
970971
f, g = c.children
971972
h, _ = d.children
972973

974+
expected_str = (
975+
" ┌─ 8\n"
976+
" ┌─ 4 ─┤\n"
977+
" ┌─ 2 ─┤ └─ \n"
978+
"─ 1 ─┤ └─ 5\n"
979+
" │ ┌─ 6\n"
980+
" └─ 3 ─┤\n"
981+
" └─ 7\n"
982+
)
983+
assert_print_statement(
984+
hprint_tree,
985+
expected_str,
986+
tree=root,
987+
)
988+
973989
class Sample:
974990
pass
975991

0 commit comments

Comments
 (0)