Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
scope.py87 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 ConsoleRenderable12 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) -> "ConsoleRenderable":23    """Render python variables in a given scope.24 25    Args:26        scope (Mapping): A mapping containing variable names and values.27        title (str, optional): Optional title. Defaults to None.28        sort_keys (bool, optional): Enable sorting of items. Defaults to True.29        indent_guides (bool, optional): Enable indentation guides. Defaults to False.30        max_length (int, optional): Maximum length of containers before abbreviating, or None for no abbreviation.31            Defaults to None.32        max_string (int, optional): Maximum length of string before truncating, or None to disable. Defaults to None.33 34    Returns:35        ConsoleRenderable: A renderable object.36    """37    highlighter = ReprHighlighter()38    items_table = Table.grid(padding=(0, 1), expand=False)39    items_table.add_column(justify="right")40 41    def sort_items(item: Tuple[str, Any]) -> Tuple[bool, str]:42        """Sort special variables first, then alphabetically."""43        key, _ = item44        return (not key.startswith("__"), key.lower())45 46    items = sorted(scope.items(), key=sort_items) if sort_keys else scope.items()47    for key, value in items:48        key_text = Text.assemble(49            (key, "scope.key.special" if key.startswith("__") else "scope.key"),50            (" =", "scope.equals"),51        )52        items_table.add_row(53            key_text,54            Pretty(55                value,56                highlighter=highlighter,57                indent_guides=indent_guides,58                max_length=max_length,59                max_string=max_string,60            ),61        )62    return Panel.fit(63        items_table,64        title=title,65        border_style="scope.border",66        padding=(0, 1),67    )68 69 70if __name__ == "__main__":  # pragma: no cover71    from rich import print72 73    print()74 75    def test(foo: float, bar: float) -> None:76        list_of_things = [1, 2, 3, None, 4, True, False, "Hello World"]77        dict_of_things = {78            "version": "1.1",79            "method": "confirmFruitPurchase",80            "params": [["apple", "orange", "mangoes", "pomelo"], 1.123],81            "id": "194521489",82        }83        print(render_scope(locals(), title="[i]locals", sort_keys=False))84 85    test(20.3423, 3.1427)86    print()87 
codekingpro/portable-devtools · Team Ai