treevalue.utils.formattree

Overview:

Library for formatting trees.

This is a copy of https://github.com/jml/tree-format, with a few modifications, its based commit id is 4c6de1074d96129b7e03eecdf42fac2cde3b5151.

Changes:
  • The NEWLINE value is modified to empty string for the vision of final tree.

  • The print_tree function is removed because it is nowhere to be used in our case.

  • Add __doc__ for format_tree function.

  • All the \n strings are replaced to os.linesep.

  • The code is reformatted.

format_tree

treevalue.utils.formattree.format_tree(node, format_node, get_children, encoding=None) → str[source]
Overview:

Format the given tree.

Arguments:
  • node: Node object

  • format_node: Format node getter

  • get_children: Children getter.

  • encoding: Encoding to be used. Default is None which means system encoding.

    When ASCII encoding is used, ASCII chars will be used instead of original chars.

Returns:
  • formatted: Formatted string.

Example:
>>> from operator import itemgetter
>>>
>>> from tree_format import format_tree
>>>
>>> tree = (
>>>     'foo', [
>>>         ('bar', [
>>>             ('a', []),
>>>             ('b', []),
>>>         ]),
>>>         ('baz', []),
>>>         ('qux', [
>>>             ('c\nd', []),
>>>         ]),
>>>     ],
>>> )
>>> format_tree(tree, format_node=itemgetter(0), get_children=itemgetter(1))
foo
├── bar
│   ├── a
│   └── b
├── baz
└── qux
    └── c
        d