1

I'm getting data from an API, and I want to validate if the data from it is there so i can use it without breaking the script. Currently I'm doing this with try blocks for each value. I'm wondering if there is a better way of writing this with DRY principles in mind.

 for record in event['data']:   
    try:
        name = record['entityDetail']['companySummary']['name']
        nameObj = {
                'name': 'name',
                'value': name,
                'type': 'S',   
            }
        objExpression.append(nameObj)
    except Exception as e:
        error = 'Company Id:'+ id + ' property ' + str(e) + ' is not defined!'
        print(error)
    try:
        brandCode = record['entityDetail']['companySummary']['brandCode']
        brandCodeObj = {
                'name': 'brandCode',
                'value': brandCode,
                'type': 'S',   
            }
        objExpression.append(brandCodeObj)
    except Exception as e:
        error = 'Company Id:'+ id + ' property ' + str(e) + ' is not defined!'
        print(error)
...
2
  • As a general rule of thumb: if you feel that you are repeating your self, an easy solution (that is also often the right one) is to put the code in a function. Commented Mar 13, 2022 at 21:00
  • Just a minor thing, but looks like you could use some f-strings - string concatenation(using +) runs slower and requires more effort! Commented Mar 14, 2022 at 2:47

1 Answer 1

1

You could use a nested for loop to repeat the logic for each field you want to add. You could use a helper function, but you'd need to pass a lot of parameters in, plus it would likely need to have the side effect of appending to a list, so I would say it's not worth it.

If you're just trying to get rid of the try blocks, you can use chained .get() calls instead. If any of the properties that you try to access don't exist, name will be assigned to None, which you can then check using an if statement.

The second parameter defines what to return in case the key doesn't exist. In this case, we return an empty dictionary, so that the remaining .get() calls can perform indexing without throwing an error. (.get() without a second parameter returns None if the corresponding key doesn't exist, hence why it's omitted for the last call in the chain.)

Here's a code snippet that implements both of these changes:

for record in event['data']:
    for field_name in ['name', 'brandCode']:   
        field_data = record.get('entityDetail', {}).get('companySummary', {}).get(field_name)
        
        if field_data:
            field_obj = {
                    'name': field_name,
                    'value': field_data,
                    'type': 'S',   
                }
            objExpression.append(field_obj)
        else:
            print("Could not fetch name!")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.