MCPcopy
hub / github.com/Textualize/rich / traverse

Function traverse

rich/pretty.py:580–875  ·  view source on GitHub ↗

Traverse object and generate a tree. Args: _object (Any): Object to be traversed. max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to None. max_string (int, optional): Maximum length of string

(
    _object: Any,
    max_length: Optional[int] = None,
    max_string: Optional[int] = None,
    max_depth: Optional[int] = None,
)

Source from the content-addressed store, hash-verified

578
579
580def traverse(
581 _object: Any,
582 max_length: Optional[int] = None,
583 max_string: Optional[int] = None,
584 max_depth: Optional[int] = None,
585) -> Node:
586 """Traverse object and generate a tree.
587
588 Args:
589 _object (Any): Object to be traversed.
590 max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.
591 Defaults to None.
592 max_string (int, optional): Maximum length of string before truncating, or None to disable truncating.
593 Defaults to None.
594 max_depth (int, optional): Maximum depth of data structures, or None for no maximum.
595 Defaults to None.
596
597 Returns:
598 Node: The root of a tree structure which can be used to render a pretty repr.
599 """
600
601 def to_repr(obj: Any) -> str:
602 """Get repr string for an object, but catch errors."""
603 if (
604 max_string is not None
605 and _safe_isinstance(obj, (bytes, str))
606 and len(obj) > max_string
607 ):
608 truncated = len(obj) - max_string
609 obj_repr = f"{obj[:max_string]!r}+{truncated}"
610 else:
611 try:
612 obj_repr = repr(obj)
613 except Exception as error:
614 obj_repr = f"<repr-error {str(error)!r}>"
615 return obj_repr
616
617 visited_ids: Set[int] = set()
618 push_visited = visited_ids.add
619 pop_visited = visited_ids.remove
620
621 def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
622 """Walk the object depth first."""
623
624 obj_id = id(obj)
625 if obj_id in visited_ids:
626 # Recursion detected
627 return Node(value_repr="...")
628
629 obj_type = type(obj)
630 children: List[Node]
631 reached_max_depth = max_depth is not None and depth >= max_depth
632
633 def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
634 for arg in rich_args:
635 if _safe_isinstance(arg, tuple):
636 if len(arg) == 3:
637 key, child, default = arg

Callers 1

pretty_reprFunction · 0.85

Calls 1

_traverseFunction · 0.85

Tested by

no test coverage detected