Skip to content

Commit 1f35bf2

Browse files
committed
v0.4.5 modify printing to omit null attributes
1 parent c9fceab commit 1f35bf2

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Work In Progress
99
- Node: WeightedNode for weighted edge tree implementation.
1010

11+
## [0.4.5] - 2022-11-08
12+
### Changed
13+
- Tree Exporter: Printing tree with added ability to omit null attributes.
14+
1115
## [0.4.4] - 2022-11-08
1216
### Fixed
1317
- Tree Constructors: Handle adding attributes that are array-like - add array even when one of the items is null
@@ -71,7 +75,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7175
- Utility Iterators: Tree traversal methods.
7276
- Workflow To Do App: Tree use case with to-do list implementation.
7377

74-
[0.4.3]: https://github.com/kayjan/bigtree/compare/v0.4.3...v0.4.4
78+
[0.4.5]: https://github.com/kayjan/bigtree/compare/v0.4.4...v0.4.5
79+
[0.4.4]: https://github.com/kayjan/bigtree/compare/v0.4.3...v0.4.4
7580
[0.4.3]: https://github.com/kayjan/bigtree/compare/v0.4.2...v0.4.3
7681
[0.4.2]: https://github.com/kayjan/bigtree/compare/v0.4.1...v0.4.2
7782
[0.4.1]: https://github.com/kayjan/bigtree/compare/v0.4.0...v0.4.1

bigtree/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.4.4"
1+
__version__ = "0.4.5"
22

33
from bigtree.dag.construct import dataframe_to_dag, dict_to_dag, list_to_dag
44
from bigtree.dag.export import dag_to_dataframe, dag_to_dict, dag_to_dot, dag_to_list

bigtree/tree/export.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def print_tree(
3434
max_depth: int = None,
3535
all_attrs: bool = False,
3636
attr_list: List[str] = None,
37+
attr_omit_null: bool = True,
3738
attr_bracket_open: str = "[",
3839
attr_bracket_close: str = "]",
3940
style: str = "ansi",
@@ -46,6 +47,7 @@ def print_tree(
4647
- Able to select which node to print from, resulting in a subtree, using `node_name`
4748
- Able to customize for maximum depth to print, using `max_depth`
4849
- Able to choose which attributes to show or show all attributes, using `attr_name_filter` and `all_attrs`
50+
- Able to omit showing of attributes if it is null, using `attr_omit_null`
4951
- Able to customize open and close brackets if attributes are shown
5052
- Able to customize style, to choose from `ansi`, `ascii`, `const`, `rounded`, `double`, and `custom` style
5153
- Default style is `ansi` style
@@ -145,6 +147,7 @@ def print_tree(
145147
max_depth (int): maximum depth of tree to print, based on `depth` attribute, optional
146148
all_attrs (bool): indicator to show all attributes, overrides `attr_list`
147149
attr_list (list): list of node attributes to print, optional
150+
attr_omit_null (bool): indicator whether to omit showing of null attributes, defaults to True
148151
attr_bracket_open (str): open bracket for `attr_list`
149152
attr_bracket_close (str): close bracket for `attr_list`
150153
style (str): style of print, defaults to abstract style
@@ -169,10 +172,23 @@ def print_tree(
169172
attr_str = ", ".join([f"{k}={v}" for k, v in attrs])
170173
attr_str = f" {attr_bracket_open}{attr_str}{attr_bracket_close}"
171174
elif attr_list:
172-
attr_str = ", ".join(
173-
[f"{attr_name}={_node.get_attr(attr_name)}" for attr_name in attr_list]
174-
)
175-
attr_str = f" {attr_bracket_open}{attr_str}{attr_bracket_close}"
175+
if attr_omit_null:
176+
attr_str = ", ".join(
177+
[
178+
f"{attr_name}={_node.get_attr(attr_name)}"
179+
for attr_name in attr_list
180+
if _node.get_attr(attr_name)
181+
]
182+
)
183+
else:
184+
attr_str = ", ".join(
185+
[
186+
f"{attr_name}={_node.get_attr(attr_name)}"
187+
for attr_name in attr_list
188+
]
189+
)
190+
if attr_str:
191+
attr_str = f" {attr_bracket_open}{attr_str}{attr_bracket_close}"
176192
node_str = f"{_node.node_name}{attr_str}"
177193
print(f"{pre_str}{fill_str}{node_str}")
178194

tests/tree/test_export.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ def test_print_tree_unknown_style(tree_node):
6262
with pytest.raises(ValueError):
6363
print_tree(tree_node, style="something")
6464

65+
@staticmethod
66+
def test_print_tree_no_attr(tree_node):
67+
expected_str = """a\n|-- b\n| |-- d\n| `-- e\n| |-- g\n| `-- h\n`-- c\n `-- f\n"""
68+
assert_print_statement(
69+
print_tree, expected_str, tree=tree_node, attr_list=["random"], style="ansi"
70+
)
71+
6572
@staticmethod
6673
def test_print_tree_child_node(tree_node):
6774
expected_str = """b\n|-- d\n`-- e\n |-- g\n `-- h\n"""
@@ -82,7 +89,11 @@ def test_print_tree_unequal_char(tree_node):
8289
def test_print_tree_attr(tree_node):
8390
expected_str = """a [age=90]\n|-- b [age=65]\n| |-- d [age=40]\n| `-- e [age=35]\n| |-- g [age=10]\n| `-- h [age=6]\n`-- c [age=60]\n `-- f [age=38]\n"""
8491
assert_print_statement(
85-
print_tree, expected_str, tree=tree_node, attr_list=["age"]
92+
print_tree,
93+
expected_str,
94+
tree=tree_node,
95+
attr_list=["age"],
96+
attr_omit_null=False,
8697
)
8798

8899
@staticmethod

0 commit comments

Comments
 (0)