typetree
Main interface for generating and visualizing an object’s type tree.
Use
Treeto generate the type tree as an object, which can be traversed as a subclass of a nested tuple.Use
print_tree()to directly print the tree view.Use
view_tree()to open the tree view as an interactive GUI.
Main classes
Tree
- class typetree.Tree(obj: ~typing.Any, *, key_text: ~typing.Optional[str] = None, max_lines: float = 1000, template: ~typing.Type[~typetree.typetree.Template] = <class 'typetree.typetree.Template'>, **kwargs)[source]
Root node of the type tree representation of a Python object.
Root-only (
Tree) attributes:- Parameters
depth (int) – Maximum depth reached
searches (int) – Total number of nodes searches
Inherited (
Subtree) attributes:- Parameters
key (str) – Key path from previous node to the current one
key_type (KeyType) – The key type used. Type help(KeyType) for more info
path (str) – The key path from the root node to the current one
type (str) – The type name of the object the node corresponds to
label (str) – The full text displayed for the node
nodes (int) – The number of inner nodes indexed
config (Template) – The settings used for generating the tree
is_expandable (bool) – Flag for whether the node has inner content. If True, the node will show as expandable in the tree view
maxed_depth (bool) – Flag for maximum depth reached. If True, the object the node refers to has inner content that was not indexed
overflowed (bool) – Flag indicating maximum branch exceeded. If True, the node has more branches than indexed. Might be from reaching either max_search or max_branches
- __init__(obj: Any, **kwargs)[source]
Build a recursive object tree structure.
- Parameters
obj (Any) – Any Python object to be analysed
key_text (str, optional) – Placeholder text for the root key node. Defaults to None
template – A configuration template for common object types. Currently supported: Template (default), DOM, HTML, and XML
items_lookup (Callable[[Any], Any], optional) – Function used to access the node’s content. Defaults to lambda var: var
type_name_lookup (Callable[[Any], Any], optional) – Function used to get the type name. Defaults to lambda var: type(var).__name__
value_lookup (Callable[[Any], Any], optional) – Function used to get the value when the node’s content is empty (tree leaves). Defaults to lambda var: var
sort_keys (bool, optional) – Flag for sorting keys alphabetically. Defaults to True
show_lengths (bool, optional) – Flag for displaying sizes of iterables. This affects how subtrees are grouped together, since Sequences of different sizes but same content types are considered equivalent. Defaults to True
include_attributes (bool, optional) – Flag for including the mutable attributes returned by vars(). Defaults to True
include_dir (bool, optional) – Flag for including the attributes returned by dir(), except the protected (_protected) and special (__special__) ones. Defaults to False
include_protected (bool, optional) – Flag for including the protected (_protected) attributes. Defaults to False
include_special` – Flag for including the special (__special__) attributes. Defaults to False
max_lines (float, optional) – 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’) or math.inf). Defaults to 1000
max_search (float, optional) – Maximum number of nodes searched. Defaults to 100,000
max_depth (float, optional) – Maximum search depth. Defaults to 20
max_branches (float, optional) – Maximum number of branches displayed on each node. This only applies after grouping. Defaults to infinity
- property depth: int
Maximum depth reached.
- print(max_lines: Optional[float] = None, verbose: bool = False)[source]
Print a tree view of the object’s type structure.
- Parameters
max_lines (float, optional) – Maximum number of lines to be printed. Can be disabled by setting it to infinity
verbose (bool, optional) – Flag for printing extra information before printing the tree view. Defaults to False
- save_as_json(file_path: str, *args, max_lines: Optional[float] = None, encoding: str = 'utf-8', ensure_ascii: bool = False, indent: int = 4, **kwargs)[source]
Save a JSON representation of the type tree to a file.
- save_as_text(file_path: str, encoding: str = 'utf-8', **kwargs)[source]
Save the tree view as a text file.
- property searches: int
Total number of nodes searched.
- to_dict(max_lines: Optional[float] = None) str | dict[str, str | dict][source]
Return a nested dict representation of the type tree.
- to_json(*args, max_lines: Optional[float] = None, **kwargs) str[source]
Return a JSON representation of the type tree.
- to_string(max_lines: Optional[float] = None, verbose: bool = False) str[source]
Get a tree view of the object’s type structure as a string.
- Parameters
max_lines (float, optional) – Maximum number of lines to be printed. Can be disabled by setting it to infinity
verbose (bool, optional) – Flag for printing extra information before printing the tree view. Defaults to False
- view(spawn_thread: bool = True, spawn_process: bool = False, max_lines: Optional[float] = None)[source]
Show a tree view of the object’s type structure in a GUI.
- Parameters
spawn_thread (bool) – Run the GUI in a separate thread
spawn_process (bool) – Run the GUI in a separate process
max_lines – Maximum number of rows to be displayed, not including the extra ellipsis at the end. Can be disabled by setting it to infinity
Subtree
- class typetree.Subtree(info_tree: _InfoTree)[source]
A recursive object tree structure.
- property is_expandable: bool
Return a flag for whether the node has inner content.
If true, the node will show as expandable in the tree view.
- property key: str
Key path from previous node to the current one.
- property key_type: KeyType
Return the key type used.
Can be a Sequence index, a Mapping key, an attribute, a Set (empty key), or none (also empty).
- property label: str
Return the full text displayed for the node.
- property maxed_depth: bool
Return a flag for maximum depth reached.
Only evaluate to True if the object the node refers to has inner content that was not indexed.
- property nodes: int
Return the number of inner nodes indexed.
- property overflowed: bool
Return a flag for maximum branch exceeded.
If True, the node has more branches than indexed. Might be from reaching either max_search or max_branches.
- property path: str
Return the key path from the root node to the current one.
- to_dict(max_lines: float) str | dict[str, str | dict][source]
Create a nested dict representing the type tree structure.
- property type: str
Return the type name of the object the node corresponds to.
Main functions
print_tree
- typetree.print_tree(obj: Any, *, max_lines: Optional[float] = None, verbose: bool = False, **kwargs)[source]
Print a tree view of the object’s type structure.
- Parameters
obj (Any) – Any Python object to be analysed
max_lines (float, optional) – Maximum number of lines to be printed. Can be disabled by setting it to infinity
verbose (bool, optional) – Flag for printing extra information before printing the tree view. Defaults to False
kwargs – Same as
Tree. Type help(Tree.__init__) for the full list
view_tree
- typetree.view_tree(obj: Any, *, spawn_thread: bool = True, spawn_process: bool = False, **kwargs)[source]
Show a tree view of the object’s type structure in a GUI.
- Parameters
obj (Any) – Any Python object to be analysed
spawn_thread (bool) – Run the GUI in a separate thread
spawn_process (bool) – Run the GUI in a separate process
kwargs – Same as
Tree. Type help(Tree.__init__) for the full list
Helper classes
Template
- class typetree.Template(items_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function get_itself>, type_name_lookup: ~collections.abc.Callable[[~typing.Any], str] = <function get_type_name>, value_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function get_itself>, 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_search: float = 100000, max_depth: float = 20, max_branches: float = inf)[source]
Default template for configuration properties of
Tree.
DOM
- class typetree.DOM(items_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function getattr_maker.<locals>.attr_get>, type_name_lookup: ~collections.abc.Callable[[~typing.Any], str] = <function getattr_maker.<locals>.attr_get>, value_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function getattr_maker.<locals>.attr_get>, 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_search: float = 100000, max_depth: float = 20, max_branches: float = inf)[source]
Template for generating a tree view of DOM etree objects.
HTML
- class typetree.HTML(items_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function get_itself>, type_name_lookup: ~collections.abc.Callable[[~typing.Any], str] = <function getattr_maker.<locals>.attr_get>, value_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function getattr_maker.<locals>.attr_get>, 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_search: float = 100000, max_depth: float = 20, max_branches: float = inf)[source]
Template for generating a tree view of HTML etree objects.
XML
- class typetree.XML(items_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function get_itself>, type_name_lookup: ~collections.abc.Callable[[~typing.Any], str] = <function getattr_maker.<locals>.attr_get>, value_lookup: ~collections.abc.Callable[[~typing.Any], ~typing.Any] = <function getattr_maker.<locals>.attr_get>, 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_search: float = 100000, max_depth: float = 20, max_branches: float = inf)[source]
Template for generating a tree view of XML etree objects.
Other
KeyType
- class typetree.KeyType(value)[source]
Node key types.
Each key will be displayed differently based on their type.
- ATTR = (1, 'Attribute')
For object attributes (starts with a dot).
Examples: .attr, ._protected, .__special__.
- INDEX = (3, 'Sequence')
For array-like Sequence indices or slices.
The indices must be consecutive integers. Examples: [2], [4:7], [:3], [:].
- MAP = (2, 'Mapping')
For dict-like Mapping keys.
Examples: [‘key’], [datetime.date(1970, 1, 1)].
- NONE = (0, 'None')
For the root node, which has no key.
- SET = (4, 'Collection')
For set-like Collections, which have item counters, but no key.
Example: (×3). The path to the item will be set to .copy().pop() for convenience, even though not all Collections accept these methods.