Skip to main content
License Python-Version Dependencies PyPI-Server

Generate a type tree view of a Python object’s contents and attributes. The subtrees with the same type pattern are grouped together as a repeating structure, which forms a much more compact tree. This is very useful, for example, for quickly identifying the overall structure of a JSON object, which often contains many repeating type patterns.

  • Includes a GUI with mouse and keyboard navigation through the nodes.

  • Has Ctrl+C/double-click support for copying paths to the inner nodes.

  • No external dependency.

Installation

Install only typetree (no external dependency):

pip install typetree

Include pyperclip for better clipboard support (optional):

pip install typetree[clipboard]

Examples

Nested iterables:

import typetree

d = [{'a', 'b', 1, 2, (3, 4), (5, 6), 'c', .1}, {'a': 0, 'b': ...}]
typetree.print_tree(d)
<list>[2]
├── [0]: <set>[8]
│   ├── (×1) <float>
│   ├── (×2) <int>
│   ├── (×2) <tuple>[2]
│   │   └── [:2]: <int>
│   └── (×3) <str>
└── [1]: <dict>[2]
    ├── ['a']: <int>
    └── ['b']: <ellipsis>

Attributes

Only the mutable attributes returned by vars() are shown by default. If you wish to view the other attributes too, use include_dir=True. This will search the dir() attributes, except the special (__special__) and the protected (_protected) ones. This can be changed by setting include_special=True and include_protected=True. Beware that this will drastically increase the tree size, so you should also limit the search depth max_depth and/or number of branches max_branches, or the application will likely freeze.

typetree.print_tree((0,), include_dir=True, max_depth=2, max_lines=14)
<tuple>[1]
├── .count: <builtin_function_or_method>
├── .index: <builtin_function_or_method>
└── [0]: <int>
    ├── .as_integer_ratio: <builtin_function_or_method>
    ├── .bit_count: <builtin_function_or_method>
    ├── .bit_length: <builtin_function_or_method>
    ├── .conjugate: <builtin_function_or_method>
    ├── .denominator: <int>
    │   └── ...
    ├── .from_bytes: <builtin_function_or_method>
    ├── .imag: <...> <int>
    ├── .numerator: <...> <int>
...

Note that the last two items have a special tag <...> which means it has identified an infinite recursion.

XML etree integration

Use type_name_lookup to specify how to retrieve the string to be displayed as the type name. End nodes of XML etrees are empty tuples, so the parameter value_lookup should also be given to specify how to retrieve their values.

import urllib.request
import xml.etree.ElementTree

url = 'https://www.w3schools.com/xml/simple.xml'
with urllib.request.urlopen(url) as response:
    r = response.read()
text = str(r, encoding='utf-8')
tree = xml.etree.ElementTree.fromstring(text)

typetree.print_tree(
    tree,
    type_name_lookup=lambda x: x.tag,
    value_lookup=lambda x: x.text,
)
<breakfast_menu>[5]
└── [:5]: <food>[4]
    ├── [0]: <name>
    ├── [1]: <price>
    ├── [2]: <description>
    └── [3]: <calories>

DOM integration

DOM objects are not directly iterable. Child nodes must be accessed through attribute lookup, which can be specified by the parameter items_lookup:

import xml.dom.minidom

dom = xml.dom.minidom.parseString(text)

typetree.print_tree(
    dom,
    items_lookup=lambda x: x.childNodes,
    type_name_lookup=lambda x: x.nodeName,
    value_lookup=lambda x: x.text,
    max_search=10,
)
<#document>[1]
├── [0]: <breakfast_menu>[11]
│   ├── [0]: <#text>
│   ├── [1]: <food>[9]
│   │   ├── [0]: <#text>
│   │   ├── [1]: <name>[1]
│   │   │   └── [0]: <#text>
│   │   ├── [2]: <#text>
│   │   ├── [3]: <price>[1]
│   │   │   ├── [0]: <#text>
│   │   │   ...
│   │   ...
│   ...
...

Alternatively, you can use configuration templates:

typetree.print_tree(dom, config=typetree.Format.DOM, max_search=15)

Which gives the same output. The extra vertical line on <#document>[1] indicates that additional items might be present, but the search was interrupted.

Interactive GUI

import json

url2 = 'https://archive.org/metadata/TheAdventuresOfTomSawyer_201303'

with urllib.request.urlopen(url2) as response2:
    r2 = response2.read()
text2 = str(r2, encoding='utf-8')
json2 = json.loads(text2)

typetree.view_tree(json2)
https://raw.githubusercontent.com/hugospinelli/typetree/master/docs/_static/GUI_Example1.png
  • Double click or press Ctrl+C to copy the path to the selected node.

  • Use right-click on the plus/minus icons to expand/collapse each of the inner nodes without affecting the node you clicked on.

  • You can use the arrow keys to navigate and the space bar instead of the right-click.

Parameters

Configuration parameters

items_lookup: Callable[[Any], Any] = lambda var: var
type_name_lookup: Callable[[Any], str] = lambda var: type(var).__name__
value_lookup: Callable[[Any], Any] = lambda var: var
sort_keys: bool = True
show_lengths: bool = True
include_attributes: bool = True
include_dir: bool = False
include_protected: bool = False
include_special: bool = False
max_branches: float = 20
max_depth: float = 10
max_search: float = 1000
max_lines: float = 1000
  • items_lookup: Function used to access the node’s content.

  • type_name_lookup: Function used to get the type name.

  • value_lookup: Function used to get the value when the node’s content is empty (tree leaves).

  • sort_keys: Flag for sorting keys alphabetically.

  • show_lengths: Flag for displaying lengths of iterables. This affects how subtrees are grouped together, since sequences with different sizes but same content types will be considered equivalent.

  • include_attributes: Flag for including the mutable attributes returned by vars.

  • include_dir: Flag for including the attributes returned by dir, except the protected (_protected) and special (__special__) ones.

  • include_protected: Flag for including the protected (_protected) attributes.

  • include_special: Flag for including the special (__special__) attributes.

  • max_branches: Maximum number of branches displayed on each node This only applies after grouping. Can be disabled by setting it to infinity (float('inf') or math.inf).

  • max_depth: Maximum display depth. Beware that the true search depth is one higher than specified. Can be disabled by setting it to infinity (not recommended).

  • max_search: Maximum number of nodes searched. Can be disabled by setting it to infinity.

  • max_lines: Maximum number of lines to be printed. For the GUI, it is the maximum number of rows to be displayed, not including the extra ellipsis at the end. Can be disabled by setting it to infinity.

Additionally, there is a helper class Format which contains configuration templates for common object types. Currently, the templates are:

  • Format.DOM

  • Format.HTML

  • Format.XML

These templates can be passed to the parameter config.

GUI

For the GUI, both the typetree.Tree(...).view method and the typetree.view_tree function accept two additional arguments to configure whether the new window is created asynchronously and by which method (threading or multiprocessing):

spawn_thread: bool = True
spawn_process: bool = False

Download files

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

Source Distribution

typetree-0.1.1.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typetree-0.1.1-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file typetree-0.1.1.tar.gz.

File metadata

  • Download URL: typetree-0.1.1.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for typetree-0.1.1.tar.gz
Algorithm Hash digest
SHA256 59fb8fb2e2508c791b9e3d6102eb15dd0ac648a23529e18b3a0087b1dfdbdb7a
MD5 9f95034fbced69932a1dc2dd52004ed9
BLAKE2b-256 a89a9733d0c08530d520094dd237d6598b97dd6afd2d33a339cc00d11159f9f0

See more details on using hashes here.

File details

Details for the file typetree-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: typetree-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for typetree-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e245e1f3cc84fecec47e6871cde77d9816c1836967b6f4a88bd4c80d02e2c223
MD5 d7a75e4a130fcc0d090b1ef87c213630
BLAKE2b-256 ed3576f2b4d401eea60404fc7cf2009d95835694233dadb46d61b19fb43c9795

See more details on using hashes here.

Supported by

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