Skip to content

Commit 91781c8

Browse files
authored
Merge pull request #287 from kayjan/feature/clean-print-kwargs
Clean print kwargs
2 parents 61e6e54 + d86f585 commit 91781c8

File tree

4 files changed

+87
-30
lines changed

4 files changed

+87
-30
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ 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.20.0] - 2024-08-24
10+
### Added:
11+
- [#285] Tree Exporter: `print_tree` and `hprint_tree` to accept keyword arguments for printing.
12+
- Misc: Template for release notes.
13+
### Changed:
14+
- Misc: Enhanced template for issues (bugfix/feature release).
815
### Fixed:
916
- Tree Exporter: `tree_to_mermaid` fix for root node to have node attribute.
1017

@@ -627,7 +634,8 @@ ignore null attribute columns.
627634
- Utility Iterator: Tree traversal methods.
628635
- Workflow To Do App: Tree use case with to-do list implementation.
629636

630-
[Unreleased]: https://github.com/kayjan/bigtree/compare/0.19.4...HEAD
637+
[Unreleased]: https://github.com/kayjan/bigtree/compare/0.20.0...HEAD
638+
[0.20.0]: https://github.com/kayjan/bigtree/compare/0.19.4...0.20.0
631639
[0.19.4]: https://github.com/kayjan/bigtree/compare/0.19.3...0.19.4
632640
[0.19.3]: https://github.com/kayjan/bigtree/compare/0.19.2...0.19.3
633641
[0.19.2]: https://github.com/kayjan/bigtree/compare/0.19.1...0.19.2

bigtree/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.19.4"
1+
__version__ = "0.20.0"
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

bigtree/tree/export.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def print_tree(
7373
attr_omit_null: bool = False,
7474
attr_bracket: List[str] = ["[", "]"],
7575
style: Union[str, Iterable[str], BasePrintStyle] = "const",
76-
**print_kwargs,
76+
**print_kwargs: Any,
7777
) -> None:
7878
"""Print tree to console, starting from `tree`.
7979
@@ -196,13 +196,14 @@ def print_tree(
196196
>>> import io
197197
>>> output = io.StringIO()
198198
>>> print_tree(root, file=output)
199-
>>> string = output.getvalue()
200-
>>> print(string)
199+
>>> tree_string = output.getvalue()
200+
>>> print(tree_string)
201201
a
202202
├── b
203203
│ ├── d
204204
│ └── e
205205
└── c
206+
<BLANKLINE>
206207
207208
Args:
208209
tree (Node): tree to print
@@ -432,7 +433,7 @@ def hprint_tree(
432433
max_depth: int = 0,
433434
intermediate_node_name: bool = True,
434435
style: Union[str, Iterable[str], BaseHPrintStyle] = "const",
435-
**print_kwargs,
436+
**print_kwargs: Any,
436437
) -> None:
437438
"""Print tree in horizontal orientation to console, starting from `tree`.
438439
@@ -527,8 +528,8 @@ def hprint_tree(
527528
>>> import io
528529
>>> output = io.StringIO()
529530
>>> hprint_tree(root, file=output)
530-
>>> string = output.getvalue()
531-
>>> print(string)
531+
>>> tree_string = output.getvalue()
532+
>>> print(tree_string)
532533
┌─ d
533534
┌─ b ─┤
534535
─ a ─┤ └─ e

tests/tree/test_export.py

+70-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import io
2+
13
import pandas as pd
24
import polars as pl
35
import pydot
@@ -23,17 +25,56 @@
2325
from tests.node.test_node import assert_tree_structure_node_root
2426
from tests.test_constants import Constants
2527

26-
tree_node_str = """a [age=90]\n├── b [age=65]\n│ ├── d [age=40]\n│ └── e [age=35]\n│ ├── g [age=10]
27-
│ └── h [age=6]\n└── c [age=60]\n └── f [age=38]\n"""
28-
tree_node_no_attr_str = """a\n├── b\n│ ├── d\n│ └── e\n│ ├── g\n│ └── h\n└── c\n └── f\n"""
29-
28+
tree_node_str = (
29+
"a [age=90]\n"
30+
"├── b [age=65]\n"
31+
"│ ├── d [age=40]\n"
32+
"│ └── e [age=35]\n"
33+
"│ ├── g [age=10]\n"
34+
"│ └── h [age=6]\n"
35+
"└── c [age=60]\n"
36+
" └── f [age=38]\n"
37+
)
38+
tree_node_no_attr_str = (
39+
"a\n"
40+
"├── b\n"
41+
"│ ├── d\n"
42+
"│ └── e\n"
43+
"│ ├── g\n"
44+
"│ └── h\n"
45+
"└── c\n"
46+
" └── f\n"
47+
)
48+
tree_node_hstr = (
49+
" ┌─ d\n"
50+
" ┌─ b ─┤ ┌─ g\n"
51+
"─ a ─┤ └─ e ─┤\n"
52+
" │ └─ h\n"
53+
" └─ c ─── f\n"
54+
)
55+
# fmt: off
56+
tree_node_branch_hstr = (
57+
" ┌─ d\n"
58+
"─ b ─┤ ┌─ g\n"
59+
" └─ e ─┤\n"
60+
" └─ h\n"
61+
)
62+
# fmt: on
3063
LOCAL = Constants.LOCAL
3164

3265

3366
class TestPrintTree:
3467
@staticmethod
3568
def test_print_tree_child_node_name(tree_node):
36-
expected_str = "b\n" "├── d\n" "└── e\n" " ├── g\n" " └── h\n"
69+
# fmt: off
70+
expected_str = (
71+
"b\n"
72+
"├── d\n"
73+
"└── e\n"
74+
" ├── g\n"
75+
" └── h\n"
76+
)
77+
# fmt: on
3778
assert_print_statement(
3879
print_tree,
3980
expected_str,
@@ -54,7 +95,15 @@ def test_print_tree_child_node_name_error(tree_node):
5495

5596
@staticmethod
5697
def test_print_tree_child_node_path(tree_node):
57-
expected_str = "b\n" "├── d\n" "└── e\n" " ├── g\n" " └── h\n"
98+
# fmt: off
99+
expected_str = (
100+
"b\n"
101+
"├── d\n"
102+
"└── e\n"
103+
" ├── g\n"
104+
" └── h\n"
105+
)
106+
# fmt: on
58107
assert_print_statement(
59108
print_tree,
60109
expected_str,
@@ -352,20 +401,19 @@ def test_print_tree_custom_style_missing_style_error(tree_node):
352401
)
353402
assert str(exc_info.value) == Constants.ERROR_NODE_EXPORT_PRINT_STYLE_SELECT
354403

404+
@staticmethod
405+
def test_print_tree_print_kwargs(tree_node):
406+
output = io.StringIO()
407+
print_tree(tree_node, file=output)
408+
assert output.getvalue() == tree_node_no_attr_str
409+
355410

356411
class TestHPrintTree:
357412
@staticmethod
358413
def test_hprint_tree(tree_node):
359-
expected_str = (
360-
" ┌─ d\n"
361-
" ┌─ b ─┤ ┌─ g\n"
362-
"─ a ─┤ └─ e ─┤\n"
363-
" │ └─ h\n"
364-
" └─ c ─── f\n"
365-
)
366414
assert_print_statement(
367415
hprint_tree,
368-
expected_str,
416+
tree_node_hstr,
369417
tree=tree_node,
370418
)
371419

@@ -473,12 +521,9 @@ def test_hprint_tree_diff_node_name_length(tree_node):
473521

474522
@staticmethod
475523
def test_hprint_tree_child_node_name(tree_node):
476-
expected_str = (
477-
" ┌─ d\n" "─ b ─┤ ┌─ g\n" " └─ e ─┤\n" " └─ h\n"
478-
)
479524
assert_print_statement(
480525
hprint_tree,
481-
expected_str,
526+
tree_node_branch_hstr,
482527
tree=tree_node,
483528
node_name_or_path="b",
484529
)
@@ -496,12 +541,9 @@ def test_hprint_tree_child_node_name_error(tree_node):
496541

497542
@staticmethod
498543
def test_hprint_tree_child_node_path(tree_node):
499-
expected_str = (
500-
" ┌─ d\n" "─ b ─┤ ┌─ g\n" " └─ e ─┤\n" " └─ h\n"
501-
)
502544
assert_print_statement(
503545
hprint_tree,
504-
expected_str,
546+
tree_node_branch_hstr,
505547
tree=tree_node,
506548
node_name_or_path="a/b",
507549
)
@@ -749,6 +791,12 @@ def test_hprint_tree_custom_style_missing_style_error(tree_node):
749791
hprint_tree(tree_node, style=["-", "+", "+", "|", "-", "="])
750792
assert str(exc_info.value) == Constants.ERROR_NODE_EXPORT_HPRINT_STYLE_SELECT
751793

794+
@staticmethod
795+
def test_hprint_tree_print_kwargs(tree_node):
796+
output = io.StringIO()
797+
hprint_tree(tree_node, file=output)
798+
assert output.getvalue() == tree_node_hstr
799+
752800

753801
class TestTreeToDataFrame:
754802
@staticmethod

0 commit comments

Comments
 (0)