python - 在函数中处理 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
解决方案
您可以编写一个函数(例如convert_to_string
),其工作是 (1) 将值更改为str
(2) 如有必要,记录错误。然后你可以使用convert_to_string
in 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
推荐阅读
- angular - 以角度创建动态getter方法,但访问时出错
- reactjs - xhr.send() 发布请求不传递数据
- python - 在 CNN 中出现错误——“‘str’对象没有属性‘decode’”
- c - C中三元运算符的求值顺序
- c++ - 转换为字符串的数字向量与文本文件中的字符串之间的这种比较不起作用?
- python - 如何根据可能的结果和频率制作一个包含 n 次出现的新列表?
- php - 在 Laravel 中将数据传递到主页的问题
- python - 带有 Flexx 的 Python 无法从“base64”导入名称“encodestring”
- regex - 匹配匹配字符串的最后一个“:”的子字符串
- xamarin - 如何使用命令而不是事件使 TapGestureRecognizer 在我的类中调用方法?