Git Product home page Git Product logo

pptree's Introduction

pptree

This package allows to pretty-print a tree of python objects.

Install

pip install pptree

or

easy_install pptree

Documentation

This package provides:

  • a default Node implementation
  • a pretty_print function accepting a default Node or custom node as root

The Node class

__init__(self, name, parent=None)
  • the name of the node to print
  • the parent Node object (optional)

The pretty-print function

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 nameattr is not filled and the custom node don't have any name field, then the str function is used. (optional)
  • whether to print the tree horizontally or vertically (optional)

Example using provided Node class

from pptree import *

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

Example using custom node implementation

class Employee:

    def __init__(self, fullname, function, head=None):
        self.fullname = fullname
        self.function = function
        self.team = []
        if head:
            head.team.append(self)

    def __str__(self):
        return self.function
jean = 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")

    ┌SalesRH
 CEO┤
    │          ┌DeveloperTechnology┤
               └Tester

>>> print_tree(jean, "team", "fullname")

            ┌Isabelle LeblancLola Monet
 Jean Dupont┤
            │            ┌Kevin PerezEnzo Riviera┤
                         └Lydia Petit

Example printing tree vertically

from pptree import *

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

Using simple binary tree realisation

The Node class

def __init__(self, value=None):
    self.value = value
    self.left = None
    self.right = None
  • the value of the node
def add(node, value):
  • add new value to the node

So you can build binary tree from the root node

The pretty-print function

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 nameattr is not filled and the custom node don't have any value field, then the str function 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')

Example

from ppbtree import *
from random import randint

root = Node()
    for _ in range(15):
        add(root, randint(10, 99))
>>> print_tree(root, nameattr='value')
      ┌1125|30|40
 46|48
   |48┘
   └51|52
      |55|     |74
      |83|  |87|  |8990

pptree's People

Contributors

clemtoy avatar hexwell avatar vladkhard avatar kanunnykov avatar newmrdenis avatar smbl64 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.