Skip to content

tree: add render_node arg to __print_backend #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,37 @@ def __init__(self, color):
sys.stdout.close()
sys.stdout = sys.__stdout__ # stops from printing to console

def test_show_render_node(self):
product_tree = Tree()

def callback_fn(node):
if node.is_leaf():
return f"A {node.data.color} {node.data.brand} car"
return node.identifier

class Product(object):
def __init__(self, brand, color):
self.brand = brand
self.color = color

product_tree.create_node(tag="root", identifier="root", data={"level": 0})
product_tree.create_node(identifier="car", parent="root", data={"level": 1})
product_tree.create_node(
identifier="car_byd", parent="car", data=Product("BYD", "red")
)
product_tree.create_node(
identifier="car_geely", parent="car", data=Product("Geely", "green")
)

self.assertEqual(
product_tree.show(stdout=False, render_node=callback_fn),
"""root
└── car
├── A red BYD car
└── A green Geely car
""",
)

def test_level(self):
self.assertEqual(self.tree.level("hárry"), 0)
depth = self.tree.depth()
Expand Down
12 changes: 11 additions & 1 deletion treelib/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def __print_backend(
reverse=False,
line_type="ascii-ex",
data_property=None,
render_node=None,
sorting=True,
func=print,
):
Expand Down Expand Up @@ -190,7 +191,10 @@ def __print_backend(
level.
"""
# Factory for proper get_label() function
if data_property:
if render_node:
get_label = render_node

elif data_property:
if idhidden:

def get_label(node):
Expand Down Expand Up @@ -842,6 +846,7 @@ def save2file(
reverse=False,
line_type="ascii-ex",
data_property=None,
render_node=None,
sorting=True,
):
"""
Expand All @@ -863,6 +868,7 @@ def handler(x):
reverse,
line_type,
data_property,
render_node,
sorting,
func=handler,
)
Expand All @@ -877,6 +883,7 @@ def show(
reverse=False,
line_type="ascii-ex",
data_property=None,
render_node=None,
stdout=True,
sorting=True,
):
Expand All @@ -902,6 +909,8 @@ def show(
:param reverse: the ``reverse`` param for sorting :class:`Node` objects in the same level.
:param line_type:
:param data_property: the property on the node data object to be printed.
:param render_node: a callback function that receives `node` and should return a string.
Useful when you want to have full control over how each node is rendered.
:param sorting: if True perform node sorting, if False return
nodes in original insertion order. In latter case @key and
@reverse parameters are ignored.
Expand All @@ -922,6 +931,7 @@ def write(line):
reverse,
line_type,
data_property,
render_node,
sorting,
func=write,
)
Expand Down