2021-04-13 04:02:29 +00:00
|
|
|
import builtins
|
2022-11-07 18:06:49 +00:00
|
|
|
import collections
|
2022-01-24 04:07:52 +00:00
|
|
|
import dataclasses
|
|
|
|
import inspect
|
2021-04-13 04:02:29 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from array import array
|
2022-01-24 04:07:52 +00:00
|
|
|
from collections import Counter, UserDict, UserList, defaultdict, deque
|
2021-04-13 04:02:29 +00:00
|
|
|
from dataclasses import dataclass, fields, is_dataclass
|
2022-01-24 04:07:52 +00:00
|
|
|
from inspect import isclass
|
2021-04-13 04:02:29 +00:00
|
|
|
from itertools import islice
|
2022-01-24 04:07:52 +00:00
|
|
|
from types import MappingProxyType
|
2021-04-13 04:02:29 +00:00
|
|
|
from typing import (
|
|
|
|
TYPE_CHECKING,
|
|
|
|
Any,
|
|
|
|
Callable,
|
2022-01-24 04:07:52 +00:00
|
|
|
DefaultDict,
|
2021-04-13 04:02:29 +00:00
|
|
|
Dict,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Optional,
|
2022-11-07 18:06:49 +00:00
|
|
|
Sequence,
|
2021-04-13 04:02:29 +00:00
|
|
|
Set,
|
|
|
|
Tuple,
|
2022-01-24 04:07:52 +00:00
|
|
|
Union,
|
2021-04-13 04:02:29 +00:00
|
|
|
)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
from rich.repr import RichReprResult
|
|
|
|
|
|
|
|
try:
|
|
|
|
import attr as _attr_module
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
_has_attrs = hasattr(_attr_module, "ib")
|
2022-11-07 18:06:49 +00:00
|
|
|
except ImportError: # pragma: no cover
|
|
|
|
_has_attrs = False
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
from . import get_console
|
|
|
|
from ._loop import loop_last
|
|
|
|
from ._pick import pick_bool
|
|
|
|
from .abc import RichRenderable
|
|
|
|
from .cells import cell_len
|
|
|
|
from .highlighter import ReprHighlighter
|
|
|
|
from .jupyter import JupyterMixin, JupyterRenderable
|
|
|
|
from .measure import Measurement
|
|
|
|
from .text import Text
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from .console import (
|
|
|
|
Console,
|
|
|
|
ConsoleOptions,
|
|
|
|
HighlighterType,
|
|
|
|
JustifyMethod,
|
|
|
|
OverflowMethod,
|
|
|
|
RenderResult,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _is_attr_object(obj: Any) -> bool:
|
|
|
|
"""Check if an object was created with attrs module."""
|
2022-11-07 18:06:49 +00:00
|
|
|
return _has_attrs and _attr_module.has(type(obj))
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
def _get_attr_fields(obj: Any) -> Sequence["_attr_module.Attribute[Any]"]:
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Get fields for an attrs object."""
|
2022-11-07 18:06:49 +00:00
|
|
|
return _attr_module.fields(type(obj)) if _has_attrs else []
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _is_dataclass_repr(obj: object) -> bool:
|
|
|
|
"""Check if an instance of a dataclass contains the default repr.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (object): A dataclass instance.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the default repr is used, False if there is a custom repr.
|
|
|
|
"""
|
|
|
|
# Digging in to a lot of internals here
|
|
|
|
# Catching all exceptions in case something is missing on a non CPython implementation
|
|
|
|
try:
|
|
|
|
return obj.__repr__.__code__.co_filename == dataclasses.__file__
|
|
|
|
except Exception: # pragma: no coverage
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
_dummy_namedtuple = collections.namedtuple("_dummy_namedtuple", [])
|
|
|
|
|
|
|
|
|
|
|
|
def _has_default_namedtuple_repr(obj: object) -> bool:
|
|
|
|
"""Check if an instance of namedtuple contains the default repr
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (object): A namedtuple
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the default repr is used, False if there's a custom repr.
|
|
|
|
"""
|
|
|
|
obj_file = None
|
|
|
|
try:
|
|
|
|
obj_file = inspect.getfile(obj.__repr__)
|
|
|
|
except (OSError, TypeError):
|
|
|
|
# OSError handles case where object is defined in __main__ scope, e.g. REPL - no filename available.
|
|
|
|
# TypeError trapped defensively, in case of object without filename slips through.
|
|
|
|
pass
|
|
|
|
default_repr_file = inspect.getfile(_dummy_namedtuple.__repr__)
|
|
|
|
return obj_file == default_repr_file
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _ipy_display_hook(
|
|
|
|
value: Any,
|
|
|
|
console: Optional["Console"] = None,
|
|
|
|
overflow: "OverflowMethod" = "ignore",
|
|
|
|
crop: bool = False,
|
|
|
|
indent_guides: bool = False,
|
|
|
|
max_length: Optional[int] = None,
|
|
|
|
max_string: Optional[int] = None,
|
2022-11-07 18:06:49 +00:00
|
|
|
max_depth: Optional[int] = None,
|
2022-01-24 04:07:52 +00:00
|
|
|
expand_all: bool = False,
|
2024-03-03 17:15:23 +00:00
|
|
|
) -> Union[str, None]:
|
2022-11-07 18:06:49 +00:00
|
|
|
# needed here to prevent circular import:
|
|
|
|
from .console import ConsoleRenderable
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
# always skip rich generated jupyter renderables or None values
|
2022-11-07 18:06:49 +00:00
|
|
|
if _safe_isinstance(value, JupyterRenderable) or value is None:
|
2024-03-03 17:15:23 +00:00
|
|
|
return None
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
console = console or get_console()
|
2024-03-03 17:15:23 +00:00
|
|
|
|
|
|
|
with console.capture() as capture:
|
|
|
|
# certain renderables should start on a new line
|
|
|
|
if _safe_isinstance(value, ConsoleRenderable):
|
|
|
|
console.line()
|
|
|
|
console.print(
|
|
|
|
value
|
|
|
|
if _safe_isinstance(value, RichRenderable)
|
|
|
|
else Pretty(
|
|
|
|
value,
|
|
|
|
overflow=overflow,
|
|
|
|
indent_guides=indent_guides,
|
|
|
|
max_length=max_length,
|
|
|
|
max_string=max_string,
|
|
|
|
max_depth=max_depth,
|
|
|
|
expand_all=expand_all,
|
|
|
|
margin=12,
|
|
|
|
),
|
|
|
|
crop=crop,
|
|
|
|
new_line_start=True,
|
|
|
|
end="",
|
|
|
|
)
|
|
|
|
# strip trailing newline, not usually part of a text repr
|
|
|
|
# I'm not sure if this should be prevented at a lower level
|
|
|
|
return capture.get().rstrip("\n")
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
def _safe_isinstance(
|
|
|
|
obj: object, class_or_tuple: Union[type, Tuple[type, ...]]
|
|
|
|
) -> bool:
|
|
|
|
"""isinstance can fail in rare cases, for example types with no __class__"""
|
|
|
|
try:
|
|
|
|
return isinstance(obj, class_or_tuple)
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2021-04-13 04:02:29 +00:00
|
|
|
def install(
|
2022-01-24 04:07:52 +00:00
|
|
|
console: Optional["Console"] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
overflow: "OverflowMethod" = "ignore",
|
|
|
|
crop: bool = False,
|
|
|
|
indent_guides: bool = False,
|
2022-01-24 04:07:52 +00:00
|
|
|
max_length: Optional[int] = None,
|
|
|
|
max_string: Optional[int] = None,
|
2022-11-07 18:06:49 +00:00
|
|
|
max_depth: Optional[int] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all: bool = False,
|
|
|
|
) -> None:
|
|
|
|
"""Install automatic pretty printing in the Python REPL.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
console (Console, optional): Console instance or ``None`` to use global console. Defaults to None.
|
|
|
|
overflow (Optional[OverflowMethod], optional): Overflow method. Defaults to "ignore".
|
|
|
|
crop (Optional[bool], optional): Enable cropping of long lines. Defaults to False.
|
|
|
|
indent_guides (bool, optional): Enable indentation guides. Defaults to False.
|
|
|
|
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 before truncating, or None to disable. Defaults to None.
|
2022-11-07 18:06:49 +00:00
|
|
|
max_depth (int, optional): Maximum depth of nested data structures, or None for no maximum. Defaults to None.
|
2022-01-24 04:07:52 +00:00
|
|
|
expand_all (bool, optional): Expand all containers. Defaults to False.
|
|
|
|
max_frames (int): Maximum number of frames to show in a traceback, 0 for no maximum. Defaults to 100.
|
2021-04-13 04:02:29 +00:00
|
|
|
"""
|
|
|
|
from rich import get_console
|
|
|
|
|
|
|
|
console = console or get_console()
|
|
|
|
assert console is not None
|
|
|
|
|
|
|
|
def display_hook(value: Any) -> None:
|
|
|
|
"""Replacement sys.displayhook which prettifies objects with Rich."""
|
|
|
|
if value is not None:
|
|
|
|
assert console is not None
|
2022-11-07 18:06:49 +00:00
|
|
|
builtins._ = None # type: ignore[attr-defined]
|
2021-04-13 04:02:29 +00:00
|
|
|
console.print(
|
|
|
|
value
|
2022-11-07 18:06:49 +00:00
|
|
|
if _safe_isinstance(value, RichRenderable)
|
2021-04-13 04:02:29 +00:00
|
|
|
else Pretty(
|
|
|
|
value,
|
|
|
|
overflow=overflow,
|
|
|
|
indent_guides=indent_guides,
|
|
|
|
max_length=max_length,
|
|
|
|
max_string=max_string,
|
2022-11-07 18:06:49 +00:00
|
|
|
max_depth=max_depth,
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all=expand_all,
|
|
|
|
),
|
|
|
|
crop=crop,
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
builtins._ = value # type: ignore[attr-defined]
|
2021-04-13 04:02:29 +00:00
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
try:
|
2022-11-07 18:06:49 +00:00
|
|
|
ip = get_ipython() # type: ignore[name-defined]
|
2024-03-03 17:15:23 +00:00
|
|
|
except NameError:
|
|
|
|
sys.displayhook = display_hook
|
|
|
|
else:
|
2021-04-13 04:02:29 +00:00
|
|
|
from IPython.core.formatters import BaseFormatter
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
class RichFormatter(BaseFormatter): # type: ignore[misc]
|
2022-01-24 04:07:52 +00:00
|
|
|
pprint: bool = True
|
|
|
|
|
|
|
|
def __call__(self, value: Any) -> Any:
|
|
|
|
if self.pprint:
|
|
|
|
return _ipy_display_hook(
|
|
|
|
value,
|
|
|
|
console=get_console(),
|
|
|
|
overflow=overflow,
|
|
|
|
indent_guides=indent_guides,
|
|
|
|
max_length=max_length,
|
|
|
|
max_string=max_string,
|
2022-11-07 18:06:49 +00:00
|
|
|
max_depth=max_depth,
|
2022-01-24 04:07:52 +00:00
|
|
|
expand_all=expand_all,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return repr(value)
|
|
|
|
|
2021-04-13 04:02:29 +00:00
|
|
|
# replace plain text formatter with rich formatter
|
2022-01-24 04:07:52 +00:00
|
|
|
rich_formatter = RichFormatter()
|
2021-04-13 04:02:29 +00:00
|
|
|
ip.display_formatter.formatters["text/plain"] = rich_formatter
|
|
|
|
|
|
|
|
|
|
|
|
class Pretty(JupyterMixin):
|
|
|
|
"""A rich renderable that pretty prints an object.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
_object (Any): An object to pretty print.
|
|
|
|
highlighter (HighlighterType, optional): Highlighter object to apply to result, or None for ReprHighlighter. Defaults to None.
|
|
|
|
indent_size (int, optional): Number of spaces in indent. Defaults to 4.
|
|
|
|
justify (JustifyMethod, optional): Justify method, or None for default. Defaults to None.
|
|
|
|
overflow (OverflowMethod, optional): Overflow method, or None for default. Defaults to None.
|
|
|
|
no_wrap (Optional[bool], optional): Disable word wrapping. Defaults to False.
|
|
|
|
indent_guides (bool, optional): Enable indentation guides. Defaults to False.
|
|
|
|
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 before truncating, or None to disable. Defaults to None.
|
2022-01-24 04:07:52 +00:00
|
|
|
max_depth (int, optional): Maximum depth of nested data structures, or None for no maximum. Defaults to None.
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all (bool, optional): Expand all containers. Defaults to False.
|
|
|
|
margin (int, optional): Subtrace a margin from width to force containers to expand earlier. Defaults to 0.
|
|
|
|
insert_line (bool, optional): Insert a new line if the output has multiple new lines. Defaults to False.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
_object: Any,
|
2022-01-24 04:07:52 +00:00
|
|
|
highlighter: Optional["HighlighterType"] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
*,
|
|
|
|
indent_size: int = 4,
|
2022-01-24 04:07:52 +00:00
|
|
|
justify: Optional["JustifyMethod"] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
overflow: Optional["OverflowMethod"] = None,
|
|
|
|
no_wrap: Optional[bool] = False,
|
|
|
|
indent_guides: bool = False,
|
2022-01-24 04:07:52 +00:00
|
|
|
max_length: Optional[int] = None,
|
|
|
|
max_string: Optional[int] = None,
|
|
|
|
max_depth: Optional[int] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all: bool = False,
|
|
|
|
margin: int = 0,
|
|
|
|
insert_line: bool = False,
|
|
|
|
) -> None:
|
|
|
|
self._object = _object
|
|
|
|
self.highlighter = highlighter or ReprHighlighter()
|
|
|
|
self.indent_size = indent_size
|
2022-01-24 04:07:52 +00:00
|
|
|
self.justify: Optional["JustifyMethod"] = justify
|
|
|
|
self.overflow: Optional["OverflowMethod"] = overflow
|
2021-04-13 04:02:29 +00:00
|
|
|
self.no_wrap = no_wrap
|
|
|
|
self.indent_guides = indent_guides
|
|
|
|
self.max_length = max_length
|
|
|
|
self.max_string = max_string
|
2022-01-24 04:07:52 +00:00
|
|
|
self.max_depth = max_depth
|
2021-04-13 04:02:29 +00:00
|
|
|
self.expand_all = expand_all
|
|
|
|
self.margin = margin
|
|
|
|
self.insert_line = insert_line
|
|
|
|
|
|
|
|
def __rich_console__(
|
|
|
|
self, console: "Console", options: "ConsoleOptions"
|
|
|
|
) -> "RenderResult":
|
|
|
|
pretty_str = pretty_repr(
|
|
|
|
self._object,
|
|
|
|
max_width=options.max_width - self.margin,
|
|
|
|
indent_size=self.indent_size,
|
|
|
|
max_length=self.max_length,
|
|
|
|
max_string=self.max_string,
|
2022-01-24 04:07:52 +00:00
|
|
|
max_depth=self.max_depth,
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all=self.expand_all,
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
pretty_text = Text.from_ansi(
|
2021-04-13 04:02:29 +00:00
|
|
|
pretty_str,
|
|
|
|
justify=self.justify or options.justify,
|
|
|
|
overflow=self.overflow or options.overflow,
|
|
|
|
no_wrap=pick_bool(self.no_wrap, options.no_wrap),
|
|
|
|
style="pretty",
|
|
|
|
)
|
|
|
|
pretty_text = (
|
|
|
|
self.highlighter(pretty_text)
|
|
|
|
if pretty_text
|
|
|
|
else Text(
|
|
|
|
f"{type(self._object)}.__repr__ returned empty string",
|
|
|
|
style="dim italic",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if self.indent_guides and not options.ascii_only:
|
|
|
|
pretty_text = pretty_text.with_indent_guides(
|
|
|
|
self.indent_size, style="repr.indent"
|
|
|
|
)
|
|
|
|
if self.insert_line and "\n" in pretty_text:
|
|
|
|
yield ""
|
|
|
|
yield pretty_text
|
|
|
|
|
|
|
|
def __rich_measure__(
|
|
|
|
self, console: "Console", options: "ConsoleOptions"
|
|
|
|
) -> "Measurement":
|
|
|
|
pretty_str = pretty_repr(
|
|
|
|
self._object,
|
|
|
|
max_width=options.max_width,
|
|
|
|
indent_size=self.indent_size,
|
|
|
|
max_length=self.max_length,
|
|
|
|
max_string=self.max_string,
|
2024-03-03 17:15:23 +00:00
|
|
|
max_depth=self.max_depth,
|
2022-11-07 18:06:49 +00:00
|
|
|
expand_all=self.expand_all,
|
2021-04-13 04:02:29 +00:00
|
|
|
)
|
|
|
|
text_width = (
|
|
|
|
max(cell_len(line) for line in pretty_str.splitlines()) if pretty_str else 0
|
|
|
|
)
|
|
|
|
return Measurement(text_width, text_width)
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _get_braces_for_defaultdict(_object: DefaultDict[Any, Any]) -> Tuple[str, str, str]:
|
2021-04-13 04:02:29 +00:00
|
|
|
return (
|
|
|
|
f"defaultdict({_object.default_factory!r}, {{",
|
|
|
|
"})",
|
|
|
|
f"defaultdict({_object.default_factory!r}, {{}})",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _get_braces_for_array(_object: "array[Any]") -> Tuple[str, str, str]:
|
2022-11-07 18:06:49 +00:00
|
|
|
return (f"array({_object.typecode!r}, [", "])", f"array({_object.typecode!r})")
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
_BRACES: Dict[type, Callable[[Any], Tuple[str, str, str]]] = {
|
|
|
|
os._Environ: lambda _object: ("environ({", "})", "environ({})"),
|
|
|
|
array: _get_braces_for_array,
|
|
|
|
defaultdict: _get_braces_for_defaultdict,
|
|
|
|
Counter: lambda _object: ("Counter({", "})", "Counter()"),
|
|
|
|
deque: lambda _object: ("deque([", "])", "deque()"),
|
|
|
|
dict: lambda _object: ("{", "}", "{}"),
|
2022-01-24 04:07:52 +00:00
|
|
|
UserDict: lambda _object: ("{", "}", "{}"),
|
2021-04-13 04:02:29 +00:00
|
|
|
frozenset: lambda _object: ("frozenset({", "})", "frozenset()"),
|
|
|
|
list: lambda _object: ("[", "]", "[]"),
|
2022-01-24 04:07:52 +00:00
|
|
|
UserList: lambda _object: ("[", "]", "[]"),
|
2021-04-13 04:02:29 +00:00
|
|
|
set: lambda _object: ("{", "}", "set()"),
|
|
|
|
tuple: lambda _object: ("(", ")", "()"),
|
2022-01-24 04:07:52 +00:00
|
|
|
MappingProxyType: lambda _object: ("mappingproxy({", "})", "mappingproxy({})"),
|
2021-04-13 04:02:29 +00:00
|
|
|
}
|
|
|
|
_CONTAINERS = tuple(_BRACES.keys())
|
2022-01-24 04:07:52 +00:00
|
|
|
_MAPPING_CONTAINERS = (dict, os._Environ, MappingProxyType, UserDict)
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_expandable(obj: Any) -> bool:
|
|
|
|
"""Check if an object may be expanded by pretty print."""
|
|
|
|
return (
|
2022-11-07 18:06:49 +00:00
|
|
|
_safe_isinstance(obj, _CONTAINERS)
|
2022-01-24 04:07:52 +00:00
|
|
|
or (is_dataclass(obj))
|
|
|
|
or (hasattr(obj, "__rich_repr__"))
|
|
|
|
or _is_attr_object(obj)
|
|
|
|
) and not isclass(obj)
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class Node:
|
|
|
|
"""A node in a repr tree. May be atomic or a container."""
|
|
|
|
|
|
|
|
key_repr: str = ""
|
|
|
|
value_repr: str = ""
|
|
|
|
open_brace: str = ""
|
|
|
|
close_brace: str = ""
|
|
|
|
empty: str = ""
|
|
|
|
last: bool = False
|
|
|
|
is_tuple: bool = False
|
2022-11-07 18:06:49 +00:00
|
|
|
is_namedtuple: bool = False
|
2021-04-13 04:02:29 +00:00
|
|
|
children: Optional[List["Node"]] = None
|
2024-03-03 17:15:23 +00:00
|
|
|
key_separator: str = ": "
|
2022-01-24 04:07:52 +00:00
|
|
|
separator: str = ", "
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
def iter_tokens(self) -> Iterable[str]:
|
|
|
|
"""Generate tokens for this node."""
|
|
|
|
if self.key_repr:
|
|
|
|
yield self.key_repr
|
|
|
|
yield self.key_separator
|
|
|
|
if self.value_repr:
|
|
|
|
yield self.value_repr
|
|
|
|
elif self.children is not None:
|
|
|
|
if self.children:
|
|
|
|
yield self.open_brace
|
2022-11-07 18:06:49 +00:00
|
|
|
if self.is_tuple and not self.is_namedtuple and len(self.children) == 1:
|
2021-04-13 04:02:29 +00:00
|
|
|
yield from self.children[0].iter_tokens()
|
|
|
|
yield ","
|
|
|
|
else:
|
|
|
|
for child in self.children:
|
|
|
|
yield from child.iter_tokens()
|
|
|
|
if not child.last:
|
2022-01-24 04:07:52 +00:00
|
|
|
yield self.separator
|
2021-04-13 04:02:29 +00:00
|
|
|
yield self.close_brace
|
|
|
|
else:
|
|
|
|
yield self.empty
|
|
|
|
|
|
|
|
def check_length(self, start_length: int, max_length: int) -> bool:
|
|
|
|
"""Check the length fits within a limit.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
start_length (int): Starting length of the line (indent, prefix, suffix).
|
|
|
|
max_length (int): Maximum length.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the node can be rendered within max length, otherwise False.
|
|
|
|
"""
|
|
|
|
total_length = start_length
|
|
|
|
for token in self.iter_tokens():
|
|
|
|
total_length += cell_len(token)
|
|
|
|
if total_length > max_length:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
repr_text = "".join(self.iter_tokens())
|
|
|
|
return repr_text
|
|
|
|
|
|
|
|
def render(
|
|
|
|
self, max_width: int = 80, indent_size: int = 4, expand_all: bool = False
|
|
|
|
) -> str:
|
|
|
|
"""Render the node to a pretty repr.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
max_width (int, optional): Maximum width of the repr. Defaults to 80.
|
|
|
|
indent_size (int, optional): Size of indents. Defaults to 4.
|
|
|
|
expand_all (bool, optional): Expand all levels. Defaults to False.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: A repr string of the original object.
|
|
|
|
"""
|
|
|
|
lines = [_Line(node=self, is_root=True)]
|
|
|
|
line_no = 0
|
|
|
|
while line_no < len(lines):
|
|
|
|
line = lines[line_no]
|
|
|
|
if line.expandable and not line.expanded:
|
|
|
|
if expand_all or not line.check_length(max_width):
|
|
|
|
lines[line_no : line_no + 1] = line.expand(indent_size)
|
|
|
|
line_no += 1
|
|
|
|
|
|
|
|
repr_str = "\n".join(str(line) for line in lines)
|
|
|
|
return repr_str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class _Line:
|
|
|
|
"""A line in repr output."""
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
parent: Optional["_Line"] = None
|
2021-04-13 04:02:29 +00:00
|
|
|
is_root: bool = False
|
|
|
|
node: Optional[Node] = None
|
|
|
|
text: str = ""
|
|
|
|
suffix: str = ""
|
|
|
|
whitespace: str = ""
|
|
|
|
expanded: bool = False
|
2022-01-24 04:07:52 +00:00
|
|
|
last: bool = False
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def expandable(self) -> bool:
|
|
|
|
"""Check if the line may be expanded."""
|
|
|
|
return bool(self.node is not None and self.node.children)
|
|
|
|
|
|
|
|
def check_length(self, max_length: int) -> bool:
|
|
|
|
"""Check this line fits within a given number of cells."""
|
|
|
|
start_length = (
|
|
|
|
len(self.whitespace) + cell_len(self.text) + cell_len(self.suffix)
|
|
|
|
)
|
|
|
|
assert self.node is not None
|
|
|
|
return self.node.check_length(start_length, max_length)
|
|
|
|
|
|
|
|
def expand(self, indent_size: int) -> Iterable["_Line"]:
|
|
|
|
"""Expand this line by adding children on their own line."""
|
|
|
|
node = self.node
|
|
|
|
assert node is not None
|
|
|
|
whitespace = self.whitespace
|
|
|
|
assert node.children
|
|
|
|
if node.key_repr:
|
2022-01-24 04:07:52 +00:00
|
|
|
new_line = yield _Line(
|
2021-04-13 04:02:29 +00:00
|
|
|
text=f"{node.key_repr}{node.key_separator}{node.open_brace}",
|
|
|
|
whitespace=whitespace,
|
|
|
|
)
|
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
new_line = yield _Line(text=node.open_brace, whitespace=whitespace)
|
2021-04-13 04:02:29 +00:00
|
|
|
child_whitespace = self.whitespace + " " * indent_size
|
|
|
|
tuple_of_one = node.is_tuple and len(node.children) == 1
|
2022-01-24 04:07:52 +00:00
|
|
|
for last, child in loop_last(node.children):
|
|
|
|
separator = "," if tuple_of_one else node.separator
|
2021-04-13 04:02:29 +00:00
|
|
|
line = _Line(
|
2022-01-24 04:07:52 +00:00
|
|
|
parent=new_line,
|
2021-04-13 04:02:29 +00:00
|
|
|
node=child,
|
|
|
|
whitespace=child_whitespace,
|
|
|
|
suffix=separator,
|
2022-01-24 04:07:52 +00:00
|
|
|
last=last and not tuple_of_one,
|
2021-04-13 04:02:29 +00:00
|
|
|
)
|
|
|
|
yield line
|
|
|
|
|
|
|
|
yield _Line(
|
|
|
|
text=node.close_brace,
|
|
|
|
whitespace=whitespace,
|
2022-01-24 04:07:52 +00:00
|
|
|
suffix=self.suffix,
|
|
|
|
last=self.last,
|
2021-04-13 04:02:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2022-01-24 04:07:52 +00:00
|
|
|
if self.last:
|
|
|
|
return f"{self.whitespace}{self.text}{self.node or ''}"
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
f"{self.whitespace}{self.text}{self.node or ''}{self.suffix.rstrip()}"
|
|
|
|
)
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
def _is_namedtuple(obj: Any) -> bool:
|
|
|
|
"""Checks if an object is most likely a namedtuple. It is possible
|
|
|
|
to craft an object that passes this check and isn't a namedtuple, but
|
|
|
|
there is only a minuscule chance of this happening unintentionally.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
obj (Any): The object to test
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: True if the object is a namedtuple. False otherwise.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
fields = getattr(obj, "_fields", None)
|
|
|
|
except Exception:
|
|
|
|
# Being very defensive - if we cannot get the attr then its not a namedtuple
|
|
|
|
return False
|
|
|
|
return isinstance(obj, tuple) and isinstance(fields, tuple)
|
|
|
|
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def traverse(
|
|
|
|
_object: Any,
|
|
|
|
max_length: Optional[int] = None,
|
|
|
|
max_string: Optional[int] = None,
|
|
|
|
max_depth: Optional[int] = None,
|
|
|
|
) -> Node:
|
2021-04-13 04:02:29 +00:00
|
|
|
"""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 before truncating, or None to disable truncating.
|
|
|
|
Defaults to None.
|
2022-01-24 04:07:52 +00:00
|
|
|
max_depth (int, optional): Maximum depth of data structures, or None for no maximum.
|
|
|
|
Defaults to None.
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Node: The root of a tree structure which can be used to render a pretty repr.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def to_repr(obj: Any) -> str:
|
|
|
|
"""Get repr string for an object, but catch errors."""
|
|
|
|
if (
|
|
|
|
max_string is not None
|
2022-11-07 18:06:49 +00:00
|
|
|
and _safe_isinstance(obj, (bytes, str))
|
2021-04-13 04:02:29 +00:00
|
|
|
and len(obj) > max_string
|
|
|
|
):
|
|
|
|
truncated = len(obj) - max_string
|
|
|
|
obj_repr = f"{obj[:max_string]!r}+{truncated}"
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
obj_repr = repr(obj)
|
|
|
|
except Exception as error:
|
2022-01-24 04:07:52 +00:00
|
|
|
obj_repr = f"<repr-error {str(error)!r}>"
|
2021-04-13 04:02:29 +00:00
|
|
|
return obj_repr
|
|
|
|
|
|
|
|
visited_ids: Set[int] = set()
|
|
|
|
push_visited = visited_ids.add
|
|
|
|
pop_visited = visited_ids.remove
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def _traverse(obj: Any, root: bool = False, depth: int = 0) -> Node:
|
2021-04-13 04:02:29 +00:00
|
|
|
"""Walk the object depth first."""
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
obj_id = id(obj)
|
|
|
|
if obj_id in visited_ids:
|
|
|
|
# Recursion detected
|
|
|
|
return Node(value_repr="...")
|
|
|
|
|
2021-04-13 04:02:29 +00:00
|
|
|
obj_type = type(obj)
|
|
|
|
children: List[Node]
|
2022-01-24 04:07:52 +00:00
|
|
|
reached_max_depth = max_depth is not None and depth >= max_depth
|
2021-04-13 04:02:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
def iter_rich_args(rich_args: Any) -> Iterable[Union[Any, Tuple[str, Any]]]:
|
2021-04-13 04:02:29 +00:00
|
|
|
for arg in rich_args:
|
2022-11-07 18:06:49 +00:00
|
|
|
if _safe_isinstance(arg, tuple):
|
2021-04-13 04:02:29 +00:00
|
|
|
if len(arg) == 3:
|
|
|
|
key, child, default = arg
|
|
|
|
if default == child:
|
|
|
|
continue
|
|
|
|
yield key, child
|
|
|
|
elif len(arg) == 2:
|
|
|
|
key, child = arg
|
|
|
|
yield key, child
|
|
|
|
elif len(arg) == 1:
|
|
|
|
yield arg[0]
|
|
|
|
else:
|
|
|
|
yield arg
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
try:
|
|
|
|
fake_attributes = hasattr(
|
|
|
|
obj, "awehoi234_wdfjwljet234_234wdfoijsdfmmnxpi492"
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
fake_attributes = False
|
|
|
|
|
|
|
|
rich_repr_result: Optional[RichReprResult] = None
|
|
|
|
if not fake_attributes:
|
|
|
|
try:
|
|
|
|
if hasattr(obj, "__rich_repr__") and not isclass(obj):
|
|
|
|
rich_repr_result = obj.__rich_repr__()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if rich_repr_result is not None:
|
2022-11-07 18:06:49 +00:00
|
|
|
push_visited(obj_id)
|
2022-01-24 04:07:52 +00:00
|
|
|
angular = getattr(obj.__rich_repr__, "angular", False)
|
|
|
|
args = list(iter_rich_args(rich_repr_result))
|
|
|
|
class_name = obj.__class__.__name__
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
if args:
|
|
|
|
children = []
|
|
|
|
append = children.append
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
if reached_max_depth:
|
2022-11-07 18:06:49 +00:00
|
|
|
if angular:
|
|
|
|
node = Node(value_repr=f"<{class_name}...>")
|
|
|
|
else:
|
|
|
|
node = Node(value_repr=f"{class_name}(...)")
|
2022-01-24 04:07:52 +00:00
|
|
|
else:
|
|
|
|
if angular:
|
|
|
|
node = Node(
|
|
|
|
open_brace=f"<{class_name} ",
|
|
|
|
close_brace=">",
|
|
|
|
children=children,
|
|
|
|
last=root,
|
|
|
|
separator=" ",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
node = Node(
|
|
|
|
open_brace=f"{class_name}(",
|
|
|
|
close_brace=")",
|
|
|
|
children=children,
|
|
|
|
last=root,
|
|
|
|
)
|
|
|
|
for last, arg in loop_last(args):
|
2022-11-07 18:06:49 +00:00
|
|
|
if _safe_isinstance(arg, tuple):
|
2022-01-24 04:07:52 +00:00
|
|
|
key, child = arg
|
|
|
|
child_node = _traverse(child, depth=depth + 1)
|
|
|
|
child_node.last = last
|
|
|
|
child_node.key_repr = key
|
|
|
|
child_node.key_separator = "="
|
|
|
|
append(child_node)
|
|
|
|
else:
|
|
|
|
child_node = _traverse(arg, depth=depth + 1)
|
|
|
|
child_node.last = last
|
|
|
|
append(child_node)
|
|
|
|
else:
|
2021-04-13 04:02:29 +00:00
|
|
|
node = Node(
|
2022-01-24 04:07:52 +00:00
|
|
|
value_repr=f"<{class_name}>" if angular else f"{class_name}()",
|
|
|
|
children=[],
|
2021-04-13 04:02:29 +00:00
|
|
|
last=root,
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
pop_visited(obj_id)
|
2022-01-24 04:07:52 +00:00
|
|
|
elif _is_attr_object(obj) and not fake_attributes:
|
2022-11-07 18:06:49 +00:00
|
|
|
push_visited(obj_id)
|
2022-01-24 04:07:52 +00:00
|
|
|
children = []
|
|
|
|
append = children.append
|
|
|
|
|
|
|
|
attr_fields = _get_attr_fields(obj)
|
|
|
|
if attr_fields:
|
|
|
|
if reached_max_depth:
|
2022-11-07 18:06:49 +00:00
|
|
|
node = Node(value_repr=f"{obj.__class__.__name__}(...)")
|
2022-01-24 04:07:52 +00:00
|
|
|
else:
|
|
|
|
node = Node(
|
|
|
|
open_brace=f"{obj.__class__.__name__}(",
|
|
|
|
close_brace=")",
|
|
|
|
children=children,
|
|
|
|
last=root,
|
|
|
|
)
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
def iter_attrs() -> (
|
|
|
|
Iterable[Tuple[str, Any, Optional[Callable[[Any], str]]]]
|
|
|
|
):
|
2022-01-24 04:07:52 +00:00
|
|
|
"""Iterate over attr fields and values."""
|
|
|
|
for attr in attr_fields:
|
|
|
|
if attr.repr:
|
|
|
|
try:
|
|
|
|
value = getattr(obj, attr.name)
|
|
|
|
except Exception as error:
|
|
|
|
# Can happen, albeit rarely
|
|
|
|
yield (attr.name, error, None)
|
|
|
|
else:
|
|
|
|
yield (
|
|
|
|
attr.name,
|
|
|
|
value,
|
|
|
|
attr.repr if callable(attr.repr) else None,
|
|
|
|
)
|
|
|
|
|
|
|
|
for last, (name, value, repr_callable) in loop_last(iter_attrs()):
|
|
|
|
if repr_callable:
|
|
|
|
child_node = Node(value_repr=str(repr_callable(value)))
|
|
|
|
else:
|
|
|
|
child_node = _traverse(value, depth=depth + 1)
|
2021-04-13 04:02:29 +00:00
|
|
|
child_node.last = last
|
2022-01-24 04:07:52 +00:00
|
|
|
child_node.key_repr = name
|
2021-04-13 04:02:29 +00:00
|
|
|
child_node.key_separator = "="
|
|
|
|
append(child_node)
|
|
|
|
else:
|
|
|
|
node = Node(
|
|
|
|
value_repr=f"{obj.__class__.__name__}()", children=[], last=root
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
pop_visited(obj_id)
|
2021-04-13 04:02:29 +00:00
|
|
|
elif (
|
|
|
|
is_dataclass(obj)
|
2022-11-07 18:06:49 +00:00
|
|
|
and not _safe_isinstance(obj, type)
|
2022-01-24 04:07:52 +00:00
|
|
|
and not fake_attributes
|
2024-03-03 17:15:23 +00:00
|
|
|
and _is_dataclass_repr(obj)
|
2021-04-13 04:02:29 +00:00
|
|
|
):
|
|
|
|
push_visited(obj_id)
|
|
|
|
children = []
|
|
|
|
append = children.append
|
2022-01-24 04:07:52 +00:00
|
|
|
if reached_max_depth:
|
2022-11-07 18:06:49 +00:00
|
|
|
node = Node(value_repr=f"{obj.__class__.__name__}(...)")
|
2022-01-24 04:07:52 +00:00
|
|
|
else:
|
|
|
|
node = Node(
|
|
|
|
open_brace=f"{obj.__class__.__name__}(",
|
|
|
|
close_brace=")",
|
|
|
|
children=children,
|
|
|
|
last=root,
|
2024-03-03 17:15:23 +00:00
|
|
|
empty=f"{obj.__class__.__name__}()",
|
2022-01-24 04:07:52 +00:00
|
|
|
)
|
2021-04-13 04:02:29 +00:00
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
for last, field in loop_last(
|
|
|
|
field for field in fields(obj) if field.repr
|
|
|
|
):
|
|
|
|
child_node = _traverse(getattr(obj, field.name), depth=depth + 1)
|
2021-04-13 04:02:29 +00:00
|
|
|
child_node.key_repr = field.name
|
|
|
|
child_node.last = last
|
|
|
|
child_node.key_separator = "="
|
|
|
|
append(child_node)
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
pop_visited(obj_id)
|
|
|
|
elif _is_namedtuple(obj) and _has_default_namedtuple_repr(obj):
|
|
|
|
push_visited(obj_id)
|
|
|
|
class_name = obj.__class__.__name__
|
|
|
|
if reached_max_depth:
|
|
|
|
# If we've reached the max depth, we still show the class name, but not its contents
|
|
|
|
node = Node(
|
|
|
|
value_repr=f"{class_name}(...)",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
children = []
|
|
|
|
append = children.append
|
|
|
|
node = Node(
|
|
|
|
open_brace=f"{class_name}(",
|
|
|
|
close_brace=")",
|
|
|
|
children=children,
|
|
|
|
empty=f"{class_name}()",
|
|
|
|
)
|
|
|
|
for last, (key, value) in loop_last(obj._asdict().items()):
|
|
|
|
child_node = _traverse(value, depth=depth + 1)
|
|
|
|
child_node.key_repr = key
|
|
|
|
child_node.last = last
|
|
|
|
child_node.key_separator = "="
|
|
|
|
append(child_node)
|
|
|
|
pop_visited(obj_id)
|
|
|
|
elif _safe_isinstance(obj, _CONTAINERS):
|
2022-01-24 04:07:52 +00:00
|
|
|
for container_type in _CONTAINERS:
|
2022-11-07 18:06:49 +00:00
|
|
|
if _safe_isinstance(obj, container_type):
|
2022-01-24 04:07:52 +00:00
|
|
|
obj_type = container_type
|
|
|
|
break
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
push_visited(obj_id)
|
|
|
|
|
|
|
|
open_brace, close_brace, empty = _BRACES[obj_type](obj)
|
|
|
|
|
2022-01-24 04:07:52 +00:00
|
|
|
if reached_max_depth:
|
2022-11-07 18:06:49 +00:00
|
|
|
node = Node(value_repr=f"{open_brace}...{close_brace}")
|
2022-01-24 04:07:52 +00:00
|
|
|
elif obj_type.__repr__ != type(obj).__repr__:
|
|
|
|
node = Node(value_repr=to_repr(obj), last=root)
|
|
|
|
elif obj:
|
2021-04-13 04:02:29 +00:00
|
|
|
children = []
|
|
|
|
node = Node(
|
|
|
|
open_brace=open_brace,
|
|
|
|
close_brace=close_brace,
|
|
|
|
children=children,
|
|
|
|
last=root,
|
|
|
|
)
|
|
|
|
append = children.append
|
|
|
|
num_items = len(obj)
|
|
|
|
last_item_index = num_items - 1
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
if _safe_isinstance(obj, _MAPPING_CONTAINERS):
|
2021-04-13 04:02:29 +00:00
|
|
|
iter_items = iter(obj.items())
|
|
|
|
if max_length is not None:
|
|
|
|
iter_items = islice(iter_items, max_length)
|
|
|
|
for index, (key, child) in enumerate(iter_items):
|
2022-01-24 04:07:52 +00:00
|
|
|
child_node = _traverse(child, depth=depth + 1)
|
2021-04-13 04:02:29 +00:00
|
|
|
child_node.key_repr = to_repr(key)
|
|
|
|
child_node.last = index == last_item_index
|
|
|
|
append(child_node)
|
|
|
|
else:
|
|
|
|
iter_values = iter(obj)
|
|
|
|
if max_length is not None:
|
|
|
|
iter_values = islice(iter_values, max_length)
|
|
|
|
for index, child in enumerate(iter_values):
|
2022-01-24 04:07:52 +00:00
|
|
|
child_node = _traverse(child, depth=depth + 1)
|
2021-04-13 04:02:29 +00:00
|
|
|
child_node.last = index == last_item_index
|
|
|
|
append(child_node)
|
|
|
|
if max_length is not None and num_items > max_length:
|
2022-11-07 18:06:49 +00:00
|
|
|
append(Node(value_repr=f"... +{num_items - max_length}", last=True))
|
2021-04-13 04:02:29 +00:00
|
|
|
else:
|
|
|
|
node = Node(empty=empty, children=[], last=root)
|
|
|
|
|
|
|
|
pop_visited(obj_id)
|
|
|
|
else:
|
|
|
|
node = Node(value_repr=to_repr(obj), last=root)
|
2022-11-07 18:06:49 +00:00
|
|
|
node.is_tuple = _safe_isinstance(obj, tuple)
|
|
|
|
node.is_namedtuple = _is_namedtuple(obj)
|
2021-04-13 04:02:29 +00:00
|
|
|
return node
|
|
|
|
|
|
|
|
node = _traverse(_object, root=True)
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_repr(
|
|
|
|
_object: Any,
|
|
|
|
*,
|
|
|
|
max_width: int = 80,
|
|
|
|
indent_size: int = 4,
|
2022-01-24 04:07:52 +00:00
|
|
|
max_length: Optional[int] = None,
|
|
|
|
max_string: Optional[int] = None,
|
|
|
|
max_depth: Optional[int] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all: bool = False,
|
|
|
|
) -> str:
|
|
|
|
"""Prettify repr string by expanding on to new lines to fit within a given width.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
_object (Any): Object to repr.
|
|
|
|
max_width (int, optional): Desired maximum width of repr string. Defaults to 80.
|
|
|
|
indent_size (int, optional): Number of spaces to indent. Defaults to 4.
|
|
|
|
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 before truncating, or None to disable truncating.
|
|
|
|
Defaults to None.
|
2022-01-24 04:07:52 +00:00
|
|
|
max_depth (int, optional): Maximum depth of nested data structure, or None for no depth.
|
|
|
|
Defaults to None.
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all (bool, optional): Expand all containers regardless of available width. Defaults to False.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: A possibly multi-line representation of the object.
|
|
|
|
"""
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
if _safe_isinstance(_object, Node):
|
2021-04-13 04:02:29 +00:00
|
|
|
node = _object
|
|
|
|
else:
|
2022-01-24 04:07:52 +00:00
|
|
|
node = traverse(
|
|
|
|
_object, max_length=max_length, max_string=max_string, max_depth=max_depth
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
repr_str: str = node.render(
|
2021-04-13 04:02:29 +00:00
|
|
|
max_width=max_width, indent_size=indent_size, expand_all=expand_all
|
|
|
|
)
|
|
|
|
return repr_str
|
|
|
|
|
|
|
|
|
|
|
|
def pprint(
|
|
|
|
_object: Any,
|
|
|
|
*,
|
2022-01-24 04:07:52 +00:00
|
|
|
console: Optional["Console"] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
indent_guides: bool = True,
|
2022-01-24 04:07:52 +00:00
|
|
|
max_length: Optional[int] = None,
|
|
|
|
max_string: Optional[int] = None,
|
|
|
|
max_depth: Optional[int] = None,
|
2021-04-13 04:02:29 +00:00
|
|
|
expand_all: bool = False,
|
2022-01-24 04:07:52 +00:00
|
|
|
) -> None:
|
2021-04-13 04:02:29 +00:00
|
|
|
"""A convenience function for pretty printing.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
_object (Any): Object to pretty print.
|
|
|
|
console (Console, optional): Console instance, or None to use default. Defaults to None.
|
|
|
|
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 strings before truncating, or None to disable. Defaults to None.
|
2022-01-24 04:07:52 +00:00
|
|
|
max_depth (int, optional): Maximum depth for nested data structures, or None for unlimited depth. Defaults to None.
|
2021-04-13 04:02:29 +00:00
|
|
|
indent_guides (bool, optional): Enable indentation guides. Defaults to True.
|
|
|
|
expand_all (bool, optional): Expand all containers. Defaults to False.
|
|
|
|
"""
|
|
|
|
_console = get_console() if console is None else console
|
|
|
|
_console.print(
|
|
|
|
Pretty(
|
|
|
|
_object,
|
|
|
|
max_length=max_length,
|
|
|
|
max_string=max_string,
|
2022-01-24 04:07:52 +00:00
|
|
|
max_depth=max_depth,
|
2021-04-13 04:02:29 +00:00
|
|
|
indent_guides=indent_guides,
|
|
|
|
expand_all=expand_all,
|
|
|
|
overflow="ignore",
|
|
|
|
),
|
|
|
|
soft_wrap=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
|
|
|
|
|
|
class BrokenRepr:
|
2022-01-24 04:07:52 +00:00
|
|
|
def __repr__(self) -> str:
|
2021-04-13 04:02:29 +00:00
|
|
|
1 / 0
|
2022-01-24 04:07:52 +00:00
|
|
|
return "this will fail"
|
2021-04-13 04:02:29 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
from typing import NamedTuple
|
|
|
|
|
|
|
|
class StockKeepingUnit(NamedTuple):
|
|
|
|
name: str
|
|
|
|
description: str
|
|
|
|
price: float
|
|
|
|
category: str
|
|
|
|
reviews: List[str]
|
|
|
|
|
2021-04-13 04:02:29 +00:00
|
|
|
d = defaultdict(int)
|
|
|
|
d["foo"] = 5
|
|
|
|
data = {
|
|
|
|
"foo": [
|
|
|
|
1,
|
|
|
|
"Hello World!",
|
|
|
|
100.123,
|
|
|
|
323.232,
|
|
|
|
432324.0,
|
|
|
|
{5, 6, 7, (1, 2, 3, 4), 8},
|
|
|
|
],
|
|
|
|
"bar": frozenset({1, 2, 3}),
|
|
|
|
"defaultdict": defaultdict(
|
|
|
|
list, {"crumble": ["apple", "rhubarb", "butter", "sugar", "flour"]}
|
|
|
|
),
|
|
|
|
"counter": Counter(
|
|
|
|
[
|
|
|
|
"apple",
|
|
|
|
"orange",
|
|
|
|
"pear",
|
|
|
|
"kumquat",
|
|
|
|
"kumquat",
|
|
|
|
"durian" * 100,
|
|
|
|
]
|
|
|
|
),
|
|
|
|
"atomic": (False, True, None),
|
2022-11-07 18:06:49 +00:00
|
|
|
"namedtuple": StockKeepingUnit(
|
|
|
|
"Sparkling British Spring Water",
|
|
|
|
"Carbonated spring water",
|
|
|
|
0.9,
|
|
|
|
"water",
|
|
|
|
["its amazing!", "its terrible!"],
|
|
|
|
),
|
2021-04-13 04:02:29 +00:00
|
|
|
"Broken": BrokenRepr(),
|
|
|
|
}
|
2022-11-07 18:06:49 +00:00
|
|
|
data["foo"].append(data) # type: ignore[attr-defined]
|
2021-04-13 04:02:29 +00:00
|
|
|
|
|
|
|
from rich import print
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
print(Pretty(data, indent_guides=True, max_string=20))
|
2022-11-07 18:06:49 +00:00
|
|
|
|
|
|
|
class Thing:
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return "Hello\x1b[38;5;239m World!"
|
|
|
|
|
|
|
|
print(Pretty(Thing()))
|