1

We got a serverless function and split the functionality in multiple smaller functions. One function transforms arrays of a dictionary in strings. This is because Azure Datafactory can't handle arrays with the build in copy activity. This is the start code:

def refactor_arrays(data, firma):
        for index, item in enumerate(data):
            data[index]['versionedRepresentations']['components']['1'] = str(
                item['versionedRepresentations']['components']['1'])

            data[index]['versionedRepresentations']['fixVersions']['1'] = str(
                item['versionedRepresentations']['fixVersions']['1'])

            data[index]['versionedRepresentations']['labels']['1'] = str(
                item['versionedRepresentations']['labels']['1'])

            data[index]['versionedRepresentations']['customfield_10005']['2'] = str(
                item['versionedRepresentations']['customfield_10005']['2'])

    return data

This is how I as a bginner would handle errors.

    def refactor_arrays(data, firma):
        for index, item in enumerate(data):
            try:
                data[index]['versionedRepresentations']['components']['1'] = str(
                    item['versionedRepresentations']['components']['1'])
            except KeyError:
                logging.info(
                    'Key does not exist. Most likely another API endpoint gets called.')
            except:
                logging.error(
                    'Something went wrong when trying to convert an array to a string.')

            try:
                data[index]['versionedRepresentations']['fixVersions']['1'] = str(
                    item['versionedRepresentations']['fixVersions']['1'])
            except KeyError:
                logging.info(
                    'Key does not exist. Most likely another API endpoint gets called.')
            except:
                logging.error(
                    'Something went wrong when trying to convert an array to a string.')

            try:
                data[index]['versionedRepresentations']['labels']['1'] = str(
                    item['versionedRepresentations']['labels']['1'])
            except KeyError:
                logging.info(
                    'Key does not exist. Most likely another API endpoint gets called.')
            except:
                logging.error(
                    'Something went wrong when trying to convert an array to a string.')

            try:
                data[index]['versionedRepresentations']['customfield_10005']['2'] = str(
                    item['versionedRepresentations']['customfield_10005']['2'])
            except KeyError:
                logging.info(
                    'Key does not exist. Most likely another API endpoint gets called.')
            except:
                logging.error(
                    'Something went wrong when trying to convert an array to a string.')

    return data
2
  • I suggest to put a check for keys in dictionary before accessing them, that way you know the problem and log it. Then put a common try catch block for whole function, no need to put try catch for every statement Commented Feb 13, 2020 at 7:04
  • Never have have an empty except block as it will also detect system interrupts and keyboard interrupts as they inherit from the base class. Commented Feb 13, 2020 at 7:13

1 Answer 1

1

You could write a function (e.g. convert_to_string) whose job is (1) to change the values to str and (2) log errors if necessary. Then you can use convert_to_string in refactor_arrays, avoiding code repetition:

def convert_to_string(data, index, a, b, c):
    try:
        data[index][a][b][c] = str(data[index][a][b][c])
    except KeyError:
        logging.info('Key does not exist. Most likely another API endpoint gets called.')
    except:
        logging.error('Something went wrong when trying to convert an array to a string.')


def refactor_arrays(data, firma):
    for index, item in enumerate(data):
        convert_to_string(data, index, 'versionedRepresentations', 'components', '1')
        convert_to_string(data, index, 'fixVersions', 'labels', '1')
        convert_to_string(data, index, 'versionedRepresentations', 'labels', '1')
        convert_to_string(data, index, 'versionedRepresentations', 'customfield_10005', '2')
    return data
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.