【发布时间】:2018-06-08 08:11:47
【问题描述】:
我从 sonarlint 看到这条消息,并试图弄清楚如何降低此功能的认知复杂性。提前感谢任何帮助。
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 工具来获取结果数字?
-
提取函数、为文件类型创建处理程序查找、统一错误处理、删除重复等。
-
你最好在Code Review发帖
标签: python python-2.7 sonarlint