caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
800 stars 185 forks source link

tree: add render_node arg to __print_backend #227

Open rix1 opened 1 month ago

rix1 commented 1 month ago

This let us do inversion of control, passing in a callback function that gives more flexibility into how the node is rendered when using show() and save2file(). Essentially replaces the need for data_property - but we should probably keep it for backwards compatibility.

The callback function right now expect a string to be returned. I was thinking on maybe supporting None which could also replace the need for the filter argument: Return None to avoid the Node to be rendered. Let me know what you think @liamlundy or @caesar0301 :)

Example:

def render_node(node):
  if node.is_leaf():
    return node.my_property
  return node.some_other_property

tree.show(render_node=render_node)

See unit test in 626f439 for an actual example.

Checklist

rix1 commented 1 month ago

Just added some unit tests.