This library provides a tree printer class for easy tree displaying
$ pip install treeprinter
from treeprinter import TreePrinter
class Tree(object):
def __init__(self, tag, children=None):
self.children = children or []
self.tag = tag
tree = Tree("animal", [
Tree("cat"),
Tree("dog", [Tree("chihuahua"), Tree("dalmatian")]),
Tree("wolf")])
printer = TreePrinter('children', 'tag')
printer.pprint(tree)
animal
|
---------------------------
| | |
cat dog wolf
|
----------
| |
chihuahua dalmatian
You could also get the same result by decorating your class like so to implement the __str__ method
@TreePrinter('children', 'tag')
class Tree(object):
def __init__(self, tag, children=None):
self.children = children or []
self.tag = tag
print(tree)
You can extend the library by implementing the base displayer (see printers/json_printer.py)
from treeprinter import JsonPrinter
printer = JsonPrinter()
json = {
"fruits": ["Apple", "Banana", "Pear"],
"presidents": {
"France": ["Sarkozy", "Hollande"],
"USA": ["Trump", "Obama"]
},
"star": "Sun"
}
printer.pprint(json)
{}
|
--------------------------------------
| | |
{presidents} [fruits] star
| | |
---------------- -------------- Sun
| | | | |
[France] [USA] Apple Banana Pear
| |
--------- ------
| | | |
Sarkozy Hollande Trump Obama