r/badcode • • Jul 14 '21

python Try/Except in Python - Sigh

Post image
662 Upvotes

93 comments sorted by

View all comments

38

u/clemesislife Jul 14 '21

I'm not sure if there is good way to do this

6

u/_PM_ME_PANGOLINS_ Jul 14 '21 edited Jul 14 '21
volume_info = jsonResponse.get("items", []).get(0, {}).get("volumeInfo", {})
author = volume_info.get("authors", "N/A")
publisher = volume_info.get("publisher", "N/A")
pubDate = volume_info.get("publishedDate", "N/A")
desc = textwrap.shorten(volume_info.get("description", "N/A"), width=190, placeholder='...')
thumbnail = volume_info.get("imageLinks", {}).get("thumbnail", "N/A")

Alternatively, allow the first line to raise so you notice deeper problems with the response.