https://github.com/inspiaaa/treeprinter
Used for printing nested tree structures
https://github.com/inspiaaa/treeprinter
Last synced: about 2 months ago
JSON representation
Used for printing nested tree structures
- Host: GitHub
- URL: https://github.com/inspiaaa/treeprinter
- Owner: Inspiaaa
- Created: 2019-12-21T11:41:22.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-21T16:58:01.000Z (over 6 years ago)
- Last Synced: 2025-03-01T21:52:24.449Z (over 1 year ago)
- Language: Python
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Treeprinter

A simple python script to recursively print a nested tree structure. It's simple to use and can support a tree structure containing user defined types.
Very useful for visualising nested data such as:
- File structure
- Abstract syntax tree
---
## Getting started
Copy the script into your local workspace and import it
```python
import tree_printer as tp
```
> If you want you can import it as `tp` because `tree_printer` can be quite annoying to type in every time you need it
To use it you have to instantiate a `TreePrinter` instance and declare a dispatch table which defines how the printer should handle each type. The dispatch table is a dictionary. To then print you must simply call the `print` method of your printer instance.
An example:
```python
import tree_printer as tp
data = {
"Eras": {
"Baroque": ["Bach", "Händel"],
"Classical": ["Mozart", "Schubert", "Beethoven"],
"Romantic": ["Chopin", "Dvorak"],
"Modern": ["Ravel"]
}
}
dispatch_table = {
dict: lambda d: [tp.Entry(key, [value]) for key, value in d.items()],
list: lambda l: [tp.Entry(value) for value in l]
}
printer = tp.TreePrinter(dispatch_table)
printer.print("History of Music", data)
```
Console:
```
History of Music
└──Eras
├──Baroque
│ ├──Bach
│ └──Händel
├──Classical
│ ├──Mozart
│ ├──Schubert
│ └──Beethoven
├──Romantic
│ ├──Chopin
│ └──Dvorak
└──Modern
└──Ravel
```
The dispatch table's keys are the type you want to handle. For example, to handle a `list` the key must be `list`. The value of it is a lambda that takes the instance of that list as an argument and returns a `tp.Entry` or a list of `tp.Entry`.
```python
{list: lambda instance: tp.Entry("Header", [])}
```
The first argument of the `Entry` constructor is the header and the second argument is a `list` of all its children.