Render python variables in a given scope. Args: scope (Mapping): A mapping containing variable names and values. title (str, optional): Optional title. Defaults to None. sort_keys (bool, optional): Enable sorting of items. Defaults to True. indent_guides (bool, o
(
scope: "Mapping[str, Any]",
*,
title: Optional[TextType] = None,
sort_keys: bool = True,
indent_guides: bool = False,
max_length: Optional[int] = None,
max_string: Optional[int] = None,
max_depth: Optional[int] = None,
overflow: Optional["OverflowMethod"] = None,
)
| 12 | |
| 13 | |
| 14 | def 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, _ = item |
| 48 | 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", |
no test coverage detected