
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Tree-like formatting for arbitrary python data structures.
pip install printree
printree
aims to be similar to pretty print (pprint) with a format inspired by the tree command:
>>> from printree import ptree, ftree
>>> ptree({"x", len, 42}) # will print to the output console
ā
āāā 0: x
āāā 1: <built-in function len>
āāā 2: 42
>>> ftree({"x", len, 42}) # will return a string representation
'ā\nāāā 0: x\nāāā 1: <built-in function len>\nāāā 2: 42'
Instances of abc.Iterable (with the exception of str & bytes) will be represented as branches. All other objects will be considered leaf nodes:
>>> from printree import ptree
>>> dct = {
... "foo": [],
... True: {
... "uno": {"ABC", "XYZ"},
... "dos": r"B:\newline\tab\like.ext",
... "tres": {
... "leaf": b"bytes",
... "numbers": (42, -17, 0.01)
... },
... },
... ("tuple", "as", "key"):
... {"multi\nlined\n\ttabbed key": "multi\nline\n\ttabbed value"}
... }
>>> dct["recursion"] = [1, dct, 2]
>>> ptree(dct)
ā
āāā foo
āāā True
ā āāā uno
ā ā āāā 0: XYZ
ā ā āāā 1: ABC
ā āāā dos: B:\newline\tab\like.ext
ā āāā tres
ā āāā leaf: b'bytes'
ā āāā numbers
ā āāā 0: 42
ā āāā 1: -17
ā āāā 2: 0.01
āāā ('tuple', 'as', 'key')
ā āāā multi
ā lined
ā tabbed key: multi
ā line
ā tabbed value
āāā recursion
āāā 0: 1
āāā 1: <Recursion on dict with id=2414949505984>
āāā 2: 2
The annotated
and depth
arguments modify verbosity of the output when creating the tree representation:
>>> ptree(dct, depth=2, annotated=True)
ā ā dict[items=4]
āāā foo ā list[empty]
āāā True ā dict[items=3]
ā āāā uno ā set[items=2] [...]
ā āāā dos: B:\newline\tab\like.ext
ā āāā tres ā dict[items=2] [...]
āāā ('tuple', 'as', 'key') ā dict[items=1]
ā āāā multi
ā lined
ā tabbed key: multi
ā line
ā tabbed value
āāā recursion ā list[items=3]
āāā 0: 1
āāā 1: <Recursion on dict with id=2414949505984>
āāā 2: 2
TreePrinter
subclasses can change each of the string representations of the tree. The subclass AsciiPrinter
is provided as an example:
>>> from printree import AsciiPrinter
>>> obj = [42, {"foo": (True, False)}]
>>> AsciiPrinter(annotated=True).ptree(obj)
. -> list[items=2]
|-- 0: 42
`-- 1 -> dict[items=1]
`-- foo -> tuple[items=2]
|-- 0: True
`-- 1: False
The main members to override are:
ROOT
EDGE
BRANCH_NEXT
BRANCH_LAST
ARROW
The level
attribute will be automatically set on the printer instance to indicate the current depth in the traversal of the tree.
To print each branch level with a different color, something like the following could be implemented:
from printree import TreePrinter
class ColoredTree(TreePrinter):
colors = {
0: '\033[31m', # red
1: '\033[32m', # green
2: '\033[33m', # yellow
3: '\033[36m', # cyan
4: '\033[35m', # magenta
}
_RESET = '\033[0m'
def __getattribute__(self, item):
if item in ("EDGE", "BRANCH_NEXT", "BRANCH_LAST"):
return f"{self.color}{getattr(super(), item)}{self._RESET}"
return super().__getattribute__(item)
@property
def color(self):
return self.colors[self.level % len(self.colors)]
@property
def ROOT(self): # for root (level 0), prefer the color of the children (level 1)
return f'{self.colors[1]}{super().ROOT}{self._RESET}'
multiline = {"foo": {False: {"AB\nCD": "xy", 42:len}, True: []}, ("bar",): []}
dct = {"A": multiline, "B": (multiline,), "C\nD": "x\ny", "F": (1, "2")}
import os
os.system("") # required on windows only
ColoredTree().ptree(dct)
Which outputs:
FAQs
Pretty print python objects in a tree format.
We found that printree demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Ā It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.