jzlas jzlas - 2 months ago 9
Python Question

How to programmatically catch which command failed on a try block

I'm trying to get some data from an JSON API. I don't want all the data that the API returns so I wrote a method that reads all the data and returns a dictionary with the relevant fields. Sometimes though, some data are missing and I would like to replace the fields that are missing with an underscore. A sample of the method is like that;

return {
'foo': data['foo'],
'bar': data['bar']
}


If a field is missing from the data, this throughs a KeyError. Is it possible to catch programmatically which field produced the error, in a single try-except block and not write a try-except block for every field?

try:
ret_dict = {
'foo': data['foo'],
'bar': data['bar']
}
except KeyError:
ret_dict[thefailurekey] = '_'


instead of

ret_dict = {}
try:
ret_dict['foo'] = data['foo']
except KeyError:
ret_dict['foo'] = '_'
try:
ret_dict['bar'] = data['bar']
except:
ret_dict['bar'] = '_'


Thank you

Answer

Use .get method of dict:

def get_data(data):
    return {
        # If you want to accept falsy values from API:
        'foo': data.get('foo', '_'),

        # If you want to override falsy values from API:
        'bar': data.get('bar') or '_',
    }

.get returns its second argument (None by default) if a dict doesn't have requested key, so it is always safe to use it in uncertain situations.

Example:

>>> data = {'foo': False, 'bar': False}
>>> get_data(data)
{'bar': '_', 'foo': False}