首页 > 解决方案 > 在函数中处理 Python 错误的最佳方法

问题描述

我们得到了一个无服务器功能,并将该功能拆分为多个较小的功能。一个函数将字典数组转换为字符串。这是因为 Azure Datafactory 无法使用内置复制活动处理数组。这是开始代码:

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

这就是我作为初学者处理错误的方式。

    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

标签: pythonerror-handling

解决方案


您可以编写一个函数(例如convert_to_string),其工作是 (1) 将值更改为str(2) 如有必要,记录错误。然后你可以使用convert_to_stringin refactor_arrays,避免代码重复:

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

推荐阅读