is there any way to add functionality to dict itself? IE - I do not want to override it - I want to create essential an extension method or mixin.
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 :(