jsonapi-client
jsonapi-client copied to clipboard
Added get(key, default=..) method to AttributeProxy, to allow dict like checking of existing variables
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.