jsonapi-client icon indicating copy to clipboard operation
jsonapi-client copied to clipboard

Added get(key, default=..) method to AttributeProxy, to allow dict like checking of existing variables

Open CaerphillyMediaLtd opened this issue 4 years ago • 0 comments

I'm not sure how welcome this is, but we've added a dict-like get method to our codebase so that we can perform typical dictionary checking for keys, e.g.:

time = response.resources.get('lastTime', '01/01/2001')

Rather than having to check if lastTime exists, then setting a default if it doesn't. As with dict it's also very useful for stringing together values:

name = resources.get('person', {}).get('name', 'Unknown')

Which dramatically improves readability, and allows you to easily default to one value if one or both of person and name don't exist.

I think this is a much more pythonic way of handling it.

CaerphillyMediaLtd avatar Mar 12 '20 19:03 CaerphillyMediaLtd