1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
from dataclasses import MISSING
from typing import Any, Sequence, TypeAlias, Union
PathPart: TypeAlias = Union[str, int, float, bool]
PathType: TypeAlias = Sequence[PathPart]
def safe_get(data: dict | list,
path: PathType,
default=MISSING,
raise_: bool = True) -> Any:
"""
Retrieve a value from a nested structure safely.
Traverses a nested structure (e.g., dictionaries or lists) following a sequence of keys or indices specified in `path`.
Handles missing keys, out-of-bounds indices, or invalid types gracefully.
Args:
data (Any): The nested structure to traverse.
path (Iterable): A sequence of keys or indices to follow.
default (Any): The value to return if the path cannot be fully traversed.
If not provided and an error occurs, the exception is re-raised.
raise_ (bool): True to raise an error on invalid path (default True).
Returns:
Any: The value at the specified path, or `default` if traversal fails.
Raises:
KeyError, IndexError, AttributeError, TypeError: If `default` is not provided
and an error occurs during traversal.
"""
...
def v1_safe_get(data: dict | list,
path: PathType,
raise_: bool) -> Any:
"""
Retrieve a value from a nested structure safely.
Traverses a nested structure (e.g., dictionaries or lists) following a sequence of keys or indices specified in `path`.
Handles missing keys, out-of-bounds indices, or invalid types gracefully.
Args:
data (Any): The nested structure to traverse.
path (Iterable): A sequence of keys or indices to follow.
raise_ (bool): True to raise an error on invalid path.
Returns:
Any: The value at the specified path, or `MISSING` if traversal fails.
Raises:
KeyError, IndexError, AttributeError, TypeError: If `default` is not provided
and an error occurs during traversal.
"""
...
def _format_err(e: Exception,
current_data: Any,
path: PathType,
current_path: PathPart):
"""Format and return a `ParseError`."""
...
def split_object_path(_input: str) -> PathType:
"""
Parse a custom object path string into a list of components.
This function interprets a custom object path syntax and breaks it into individual path components,
including dictionary keys, list indices, attributes, and nested elements.
It handles escaped characters and supports mixed types (e.g., strings, integers, floats, booleans).
Args:
_input (str): The object path string to parse.
Returns:
PathType: A list of components representing the parsed path. Components can be strings,
integers, floats, booleans, or other valid key/index types.
Example:
>>> split_object_path(r'''a[b][c]["d\\\"o\\\""][e].f[go]['1'].then."y\\e\\\"s"[1]["we can!"].five.2.3.[ok][4.56].[-7.89].'let\\'sd\\othisy\\'all!'.yeah.123.False['True'].thanks!''')
['a', 'b', 'c', 'd"o"', 'e', 'f', 'go', '1', 'then', 'y\\e"s', 1, 'we can!', 'five', 2, 3, 'ok', 4.56, -7.89,
"let'sd\\othisy'all!", 'yeah', 123, False, 'True', 'thanks!']
"""
|