Package: typetree
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.InstallationInstall only typetree (no external dependency):pip install typetreeIncludepyperclipfor better clipboard support (optional):pip install typetree[clipboard]ExamplesNested iterables:importtypetreed=[{'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>AttributesOnly the mutable attributes returned byvars()are shown by default.
If you wish to view the other attributes too, useinclude_dir=True.
This will search thedir()attributes, except the special
(__special__) and the protected (_protected) ones.
This can be changed by settinginclude_special=Trueandinclude_protected=True. Beware that this will drastically increase
the tree size, so you should also limit the search depthmax_depthand/or number of branchesmax_branches, or the application will
likely freeze.typetree.print_tree((0,),include_dir=True,max_depth=2,max_lines=15)<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 integrationUsetype_name_lookupto specify how to retrieve the string to be
displayed as the type name. End nodes of XML etrees are empty tuples, so
the parametervalue_lookupshould also be given to specify how to
retrieve their values.importurllib.requestimportxml.etree.ElementTreeurl='https://www.w3schools.com/xml/simple.xml'withurllib.request.urlopen(url)asresponse:r=response.read()text=str(r,encoding='utf-8')tree=xml.etree.ElementTree.fromstring(text)typetree.print_tree(tree,type_name_lookup=lambdax:x.tag,value_lookup=lambdax:x.text,)<breakfast_menu>[5]
└── [:5]: <food>[4]
    ├── [0]: <name>
    ├── [1]: <price>
    ├── [2]: <description>
    └── [3]: <calories>DOM integrationDOM objects are not directly iterable. Child nodes must be accessed through
attribute lookup, which can be specified by the parameteritems_lookup:importxml.dom.minidomdom=xml.dom.minidom.parseString(text)typetree.print_tree(dom,items_lookup=lambdax:x.childNodes,type_name_lookup=lambdax:x.nodeName,value_lookup=lambdax:x.text,max_lines=10,)<#document>[1]
└── [0]: <breakfast_menu>[11]
    ├── [0]: <#text>
    ├── [1]: <food>[9]
    │   ├── [0]: <#text>
    │   ├── [1]: <name>[1]
    │   │   └── [0]: <#text>
    │   ├── [2]: <#text>
    │   ├── [3]: <price>[1]
...Alternatively, you can use configuration templates:typetree.print_tree(dom,template=typetree.DOM,max_lines=10)Which gives the same output.Interactive GUIimportjsonurl2='https://archive.org/metadata/TheAdventuresOfTomSawyer_201303'withurllib.request.urlopen(url2)asresponse2:r2=response2.read()text2=str(r2,encoding='utf-8')json2=json.loads(text2)typetree.view_tree(json2)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.ParametersConfiguration parametersitems_lookup:Callable[[Any],Any]=lambdavar:vartype_name_lookup:Callable[[Any],str]=lambdavar:type(var).__name__value_lookup:Callable[[Any],Any]=lambdavar:varsort_keys:bool=Trueshow_lengths:bool=Trueinclude_attributes:bool=Trueinclude_dir:bool=Falseinclude_protected:bool=Falseinclude_special:bool=Falsemax_lines:float=1000max_search:float=100_000max_depth:float=20max_branches:float=float('inf')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 are considered equivalent.include_attributes: Flag for including the mutable attributes
returned byvars.include_dir: Flag for including the attributes returned bydir, except the protected (_protected) and the special
(__special__) ones.include_protected: Flag for including the protected
(_protected) attributes.include_special: Flag for including the special
(__special__) attributes.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
(float('inf')ormath.inf).max_search: Maximum number of nodes searched.max_depth: Maximum search depth.max_branches: Maximum number of branches displayed on each
node. This only applies after grouping.Additionally, there are also helper classes of configuration templates for
common object types. Currently, the templates are:Template(default)DOMHTMLXMLThese templates can be passed to the parametertemplate.GUIFor the GUI, both theTree(...).viewmethod and theview_treefunction accept two additional arguments to configure
whether the new window is created asynchronously and by which method
(threading or multiprocessing):spawn_thread:bool=Truespawn_process:bool=False