This package allows to pretty-print a tree of python objects.
pip install pptree
or
easy_install pptree
This package provides:
- a default
Nodeimplementation - a
pretty_printfunction accepting a defaultNodeor custom node as root
__init__(self, name, parent=None)- the name of the node to print
- the parent
Nodeobject (optional)
print_tree(current_node, childattr='children', nameattr='name', horizontal=True)- the root node object
- the name of the list containing the children (optional)
- the name of the field containing the text to display. If
nameattris not filled and the custom node don't have anynamefield, then thestrfunction is used. (optional) - whether to print the tree horizontally or vertically (optional)
frompptreeimport*shame=Node("shame")
conscience=Node("conscience", shame)
selfdisgust=Node("selfdisgust", shame)
embarrassment=Node("embarrassment", shame)
selfconsciousness=Node("selfconsciousness", embarrassment)
shamefacedness=Node("shamefacedness", embarrassment)
chagrin=Node("chagrin", embarrassment)
discomfiture=Node("discomfiture", embarrassment)
abashment=Node("abashment", embarrassment)
confusion=Node("confusion", embarrassment)
print_tree(shame)Output:
┌conscience
├self-disgust
shame┤
│ ┌self-consciousness
│ ├shamefacedness
│ ├chagrin
└embarrassment┤
├discomfiture
├abashment
└confusion
classEmployee:
def__init__(self, fullname, function, head=None):
self.fullname=fullnameself.function=functionself.team= []
ifhead:
head.team.append(self)
def__str__(self):
returnself.functionjean=Employee("Jean Dupont", "CEO")
isabelle=Employee("Isabelle Leblanc", "Sales", jean)
enzo=Employee("Enzo Riviera", "Technology", jean)
lola=Employee("Lola Monet", "RH", jean)
kevin=Employee("Kevin Perez", "Developer", enzo)
lydia=Employee("Lydia Petit", "Tester", enzo)>>>print_tree(jean, "team")
┌Sales
├RHCEO┤
│ ┌Developer
└Technology┤
└Tester>>>print_tree(jean, "team", "fullname")
┌IsabelleLeblanc
├LolaMonetJeanDupont┤
│ ┌KevinPerez
└EnzoRiviera┤
└LydiaPetitfrompptreeimport*shame=Node("shame")
conscience=Node("conscience", shame)
selfdisgust=Node("selfdisgust", shame)
embarrassment=Node("embarrassment", shame)
selfconsciousness=Node("selfconsciousness", embarrassment)
shamefacedness=Node("shamefacedness", embarrassment)
chagrin=Node("chagrin", embarrassment)
discomfiture=Node("discomfiture", embarrassment)
abashment=Node("abashment", embarrassment)
confusion=Node("confusion", embarrassment)
print_tree(shame, horizontal=False)Output:
shame ┌─────────────┬──────┴─────────────────────────────────────────────┐ conscience selfdisgust embarrassment ┌─────────────────┬─────────────┬────┴─────┬───────────┬────────────┐ selfconsciousness shamefacedness chagrin confusion abashment discomfiture
def__init__(self, value=None):
self.value=valueself.left=Noneself.right=None- the value of the node
defadd(node, value):- add new value to the node
So you can build binary tree from the root node
print_tree(current_node, nameattr='value', left_child='left', right_child='right')- the root node object
- the name of the field containing the text to display. If
nameattris not filled and the custom node don't have anyvaluefield, then thestrfunction is used. (optional) - the left child attribute name
- the right child attribute name
print_tree recursively prints current_node.left and current_node.right elements, so you need to call it only with root node, like
>>>print_tree(root_node, nameattr='value')fromppbtreeimport*fromrandomimportrandintroot=Node()
for_inrange(15):
add(root, randint(10, 99))>>>print_tree(root, nameattr='value')
┌11
┌25┤
| └30┐
| └4046┤
| ┌48| ┌48┘
└51┤
| ┌52| ┌55┤
|| └74| ┌83┤
|| └87┐
|| └89
└90┘