Is there any way to add functionality to dict itself? There are a log of reasons I would like to do this - but for instance - I'd like to do something like:
def safe_get( the_dictionary, key ):
return None if the_dictionary is None else the_dictionary.get(key)
# this does NOT work, because dict is a built in type
dict.safe_get = safe_get
Because this is tolerable:
result = event.safe_get("request_context").safe_get("identity").safe_get("sourceIp")
And this is ridiculous:
result = safe_get( safe_get( safe_get( event, 'request_context'), 'identity'), 'sourceIp')
Or is there another (shorter == better) way of doing the same thing?
When I google I see people defining their own classes and things - but I don't create "event" - it comes from amazon, so I have no control over it, and can't change it to another type. I could wrap it - but that would be even more code :(