Last active
September 19, 2023 13:28
-
-
Save milosb793/80610e15f5c547e29558e466c65cbe22 to your computer and use it in GitHub Desktop.
Safe attribute accessor for (almost) all types - Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get(obj, key: str | int, default=None): | |
""" | |
Try to get index, key or a property | |
for a given list, dict or object, | |
returns default on error | |
Params: | |
- obj: mixed - dict, list, object | |
- key: str - either any key or dot accessor like: foo.bar.0 | |
Returns: | |
- mixed | None | |
""" | |
key = str(key) | |
keys = [key] | |
if '.' in key: | |
keys = key.split('.') | |
val = obj | |
for k in keys: | |
if not k: | |
continue | |
try: | |
if isinstance(val, list): | |
val = val[k] | |
elif isinstance(val, dict): | |
val = val.get(k, default) | |
else: | |
val = getattr(val, k, default) | |
except (IndexError, KeyError, AttributeError, TypeError, ValueError) as e: | |
if 'list indices must be integers or slices, not str' in str(e): | |
try: | |
k = int(k) | |
val = val[k] | |
except (ValueError, KeyError, IndexError): | |
val = default | |
else: | |
return default | |
return val |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment