Skip to content

Instantly share code, notes, and snippets.

@piotr-piatkowski
Last active November 9, 2021 11:43
Show Gist options
  • Save piotr-piatkowski/7a1a16df7e84ed476bbe3b160e742762 to your computer and use it in GitHub Desktop.
Save piotr-piatkowski/7a1a16df7e84ed476bbe3b160e742762 to your computer and use it in GitHub Desktop.
def get_by_keys(res, *keys):
try:
for k in keys:
res = res[k]
return res
except (TypeError, KeyError, IndexError):
return None
book_data = {
'title': get_by_keys(result, 'volumeInfo', 'title'),
'authors': get_by_keys(result, 'volumeInfo', 'authors', 0),
'publish_date': get_by_keys(result, 'volumeInfo', 'publishedDate'),
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment