我从sonarlint看到此消息并试图找出如何减少此功能的认知复杂性。任何援助提前赞赏。Python重构此功能以将其认知复杂度从19降低到15允许
import os
import json
import click
import hcl
cfn = [".json", ".template", ".yaml", ".yml"]
tf = ["tf"]
def file_handler(dir):
for root, dirs, files in os.walk(dir):
for file in files:
if file.endswith(tuple(cfn)):
with open(os.path.join(root, file), 'r') as fin:
try:
file = fin.read()
if "AWSTemplateFormatVersion" in file:
data = json.dumps(file)
print(data)
except ValueError as e:
raise SystemExit(e)
elif file.endswith(tuple(tf)):
with open(os.path.join(root, file), 'r') as file:
try:
obj = hcl.load(file)
data = json.dumps(obj)
print(data)
except ValueError as e:
raise SystemExit(e)
return data
你使用什么metrix工具来获得结果数字? – SteveJ
提取函数,为文件类型创建处理程序查找,统一错误处理,删除重复等,等等。 –
您可能更好地张贴在[codereview.se] –