at()
Retrieves the value at the given path of the object.
Implementation
The path value can be a string or an array of strings or integers. If a path value is an array, it will be used to traverse an array-liked object. Args: obj: The object to retrieve values from. *paths: The path(s) to retrieve values from. Returns: A list of values at the given paths.
Example
at({'a': {'b': 2}}, 'a.b')
Expected output: [2]
Source Code
def at(obj: Dict[Any, Any], *paths: str) -> List[Any]:
results: List[Any] = []
for path in paths:
current = obj
# split on dots and brackets
parts = re.findall(r"[^.\[\]]+", path)
for p in parts:
if isinstance(current, dict):
current = current.get(p)
elif isinstance(current, (list, tuple)):
try:
idx = int(p)
current = current[idx]
except Exception:
current = None
else:
current = None
if current is None:
break
results.append(current)
return results