我從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] –