Skip to content

Instantly share code, notes, and snippets.

@technillogue
Created March 12, 2021 20:05
Show Gist options
  • Select an option

  • Save technillogue/bee0b084c80f2a658abaf53724afb735 to your computer and use it in GitHub Desktop.

Select an option

Save technillogue/bee0b084c80f2a658abaf53724afb735 to your computer and use it in GitHub Desktop.
find stuff in overly informative API responses
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