Skip to main content

Pretty print python objects in a tree format.

Project description

printree

Tree-like formatting for python containers.

Similar to pretty print (pprint) but in the form of a tree:

>>> from printree import ptree, ftree
>>> ptree({"x", len, 42})  # will print to the output console
`- . [items=3]
   |- 0: <built-in function len>
   |- 1: 42
   `- 2: x
>>> ftree({"x", len, 42})  # will return a string representation
'`- . [items=3]\n   |- 0: <built-in function len>\n   |- 1: 42\n   `- 2: x'

Instances of abc.Iterable (with the exception of str & bytes) should be translated into a tree-like form. Other objects will be considered "leaf nodes":

>>> dct = {
...     "multi\nlined\n\ttabbed key": 1,
...     True: {
...         "uno": {"A", "B", "C"},
...         "dos": r"B:\newline\tab\like.ext",
...         "tres": {
...             "leaf": "string",
...             "numbers": (42, -17, 0.01)
...         },
...     },
...     "foo": [],
...     ("unsortable", ("tuple", "as", "key")):
...         ["multi\nline\nfirst", "multi\nline\nlast"]
... }
>>> dct['recursive_reference'] = dct
>>> ptree(dct)
`- . [items=5]
   |- multi
   |  lined
   |    tabbed key: 1
   |- True [items=3]
   |  |- dos: B:\newline\tab\like.ext
   |  |- tres [items=2]
   |  |  |- leaf: string
   |  |  `- numbers [items=3]
   |  |     |- 0: -17
   |  |     |- 1: 0.01
   |  |     `- 2: 42
   |  `- uno [items=3]
   |     |- 0: A
   |     |- 1: B
   |     `- 2: C
   |- foo [empty]
   |- ('unsortable', ('tuple', 'as', 'key')) [items=2]
   |  |- 0: multi
   |  |     line
   |  |     first
   |  `- 1: multi
   |        line
   |        last
   `- recursive_reference: <Recursion on dict with id=140712966998864>

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

printree-0.1.0.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

printree-0.1.0-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

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