Created
March 12, 2021 20:05
-
-
Save technillogue/bee0b084c80f2a658abaf53724afb735 to your computer and use it in GitHub Desktop.
find stuff in overly informative API responses
This file contains hidden or 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 walk(obj, path): | |
| last = obj | |
| for key in path: | |
| if isinstance(last, list): | |
| last = last[key] | |
| else: | |
| last = last.get(key, {}) | |
| def prune(obj, keys=["Name", "DisplayMessage", "Type", "Time"]): | |
| if isinstance(obj, (list, tuple)): | |
| return [ | |
| maybe_child for item in obj if (maybe_child := prune(item, keys)) | |
| ] | |
| elif isinstance(obj, dict): | |
| result = {} | |
| for k, v in obj.items(): | |
| if k in keys: | |
| result[k] = v | |
| if isinstance(v, (dict, list, tuple)): | |
| maybe_child = prune(v, keys) | |
| if maybe_child: | |
| result[k] = maybe_child | |
| return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment