gpt4 book ai didi

python - 处理函数中 Python 错误的最佳方法

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:56 24 4
gpt4 key购买 nike

我们获得了一个无服务器功能,并将该功能拆分为多个较小的功能。一个函数将字典数组转换为字符串。这是因为 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

这就是我作为一名 bginner 处理错误的方式。

    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

最佳答案

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

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

关于python - 处理函数中 Python 错误的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60202026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com