Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
scope.py93 linesDownload Raw Back to rich
1from collections.abc import Mapping2from typing import TYPE_CHECKING, Any, Optional, Tuple3 4from .highlighter import ReprHighlighter5from .panel import Panel6from .pretty import Pretty7from .table import Table8from .text import Text, TextType9 10if TYPE_CHECKING:11    from .console import ConsoleRenderable, OverflowMethod12 13 14def render_scope(15    scope: "Mapping[str, Any]",16    *,17    title: Optional[TextType] = None,18    sort_keys: bool = True,19    indent_guides: bool = False,20    max_length: Optional[int] = None,21    max_string: Optional[int] = None,22    max_depth: Optional[int] = None,23    overflow: Optional["OverflowMethod"] = None,24) -> "ConsoleRenderable":25    """Render python variables in a given scope.26 27    Args:28        scope (Mapping): A mapping containing variable names and values.29        title (str, optional): Optional title. Defaults to None.30        sort_keys (bool, optional): Enable sorting of items. Defaults to True.31        indent_guides (bool, optional): Enable indentation guides. Defaults to False.32        max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.33            Defaults to None.34        max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.35        max_depth (int, optional): Maximum depths of locals before truncating, or None to disable. Defaults to None.36        overflow (OverflowMethod, optional): How to handle overflowing locals, or None to disable. Defaults to None.37 38    Returns:39        ConsoleRenderable: A renderable object.40    """41    highlighter = ReprHighlighter()42    items_table = Table.grid(padding=(0, 1), expand=False)43    items_table.add_column(justify="right")44 45    def sort_items(item: Tuple[str, Any]) -> Tuple[bool, str]:46        """Sort special variables first, then alphabetically."""47        key, _ = item48        return (not key.startswith("__"), key.lower())49 50    items = sorted(scope.items(), key=sort_items) if sort_keys else scope.items()51    for key, value in items:52        key_text = Text.assemble(53            (key, "scope.key.special" if key.startswith("__") else "scope.key"),54            (" =", "scope.equals"),55        )56        items_table.add_row(57            key_text,58            Pretty(59                value,60                highlighter=highlighter,61                indent_guides=indent_guides,62                max_length=max_length,63                max_string=max_string,64                max_depth=max_depth,65                overflow=overflow,66            ),67        )68    return Panel.fit(69        items_table,70        title=title,71        border_style="scope.border",72        padding=(0, 1),73    )74 75 76if __name__ == "__main__":  # pragma: no cover77    from rich import print78 79    print()80 81    def test(foo: float, bar: float) -> None:82        list_of_things = [1, 2, 3, None, 4, True, False, "Hello World"]83        dict_of_things = {84            "version": "1.1",85            "method": "confirmFruitPurchase",86            "params": [["apple", "orange", "mangoes", "pomelo"], 1.123],87            "id": "194521489",88        }89        print(render_scope(locals(), title="[i]locals", sort_keys=False))90 91    test(20.3423, 3.1427)92    print()93 
codekingpro/portable-devtools · Team Ai