Skip to main content

Python library for easy tree displaying

Project description

Tree printer
============

This library provides a tree printer class for easy tree displaying

## Installation
``` sh
$ pip install treeprinter
```

## Basic usage

### Printer instance
``` python
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
```

### Printer decorator
You could also get the same result by decorating your class like so to implement the \_\_str\_\_ method
``` python
@TreePrinter('children', 'tag')
class Tree(object):
def __init__(self, tag, children=None):
self.children = children or []
self.tag = tag

print(tree)
```


## Extensions
You can extend the library by implementing the base displayer
(see printers/json_printer.py)

``` python
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
```

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treeprinter-0.0.1.tar.gz (6.8 kB view details)

Uploaded Source

File details

Details for the file treeprinter-0.0.1.tar.gz.

File metadata

  • Download URL: treeprinter-0.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for treeprinter-0.0.1.tar.gz
Algorithm Hash digest
SHA256 35bfcc931d0d267963ea933de368242cb2e19d848da553f912be53e900601643
MD5 803db9dca53a41c88d080a5c61682edf
BLAKE2b-256 29da9c8ce8e9febe30d4d5a2406766df9d4a4f4dc57b123b5cc8562abc822d5e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page