这里是python的新手。我有检查文件元素的要求。 我的代码是这样的:
import json, os, warnings
file='file.json'
id_list=[]
def file_accessible(file):
if os.path.exists(file) :
try:
f = open(file)
f.close()
except IOError:
return False
else:
return True
def valid_jsonfile(file):
with open(file) as json_file:
try:
json.load(json_file)
except ValueError as e:
return False
else:
return True
def open_file(file):
with open(file) as json_file:
data= json.load(json_file)
return data
def access_dict(data):
if isinstance(data, dict):
for key in data.keys():
return data.keys()
def is_empty(dict_elem):
if dict_elem=="":
return True
else:
return False
def is_integer(dict_elem):
if not is_empty(dict_elem):
if not isinstance(dict_elem,bool):
if isinstance(dict_elem,int):
return True
else:
# raise TypeError('Value of given key is not integer')
return False
else:
return False
else:
# raise ValueError('Empty value')
return False
def check_id(dict_elem):
if is_integer(dict_elem):
if not id_list:
id_list.append(dict_elem)
return True
else:
if dict_elem in id_list:
#return ValueError('id not unique, already exist.')
return False
else:
id_list.append(dict_elem)
return True
def gt_zero (dict_elem):
if is_integer(dict_elem):
if dict_elem > 0:
return True
else:
return False
elif isinstance(dict_elem,(dict,list)):
if len(dict_elem)>0:
return True
else:
return False
else:
# raise TypeError('Value of given key is not integer')
return False
def check_bool(dict_elem):
if isinstance(dict_elem, bool):
return True
else:
# raise TypeError('Not a boolean value.')
return False
def not_negative(dict_elem):
if is_integer(dict_elem):
if dict_elem>=0:
return True
else:
# raise ValueError('negative value')
return False
else:
# raise TypeError('Value of given key is not integer')
return False
def main():
file_accessible(file)
valid_jsonfile(file)
data= open_file(file)
n=len(data['dict1'])
for i in range(n):
keys= access_dict(data['dict1'][i])
for k in keys:
is_empty(data['dict1'][i][k])
check_id(data['dict1'][i]['id'])
check_bool(data['dict1'][i]['bool_var1'])
check_bool(data['dict1'][i]['bool_var2'])
is_integer(data['dict1'][i]['var1'])
not_negative(data['dict1'][i]['var1'])
gt_zero(data['dict1'][i]['nested_dict1'])
我的问题是,如果函数中的所有函数都成功,我需要main返回true;如果其中的1个函数返回false或发生错误,则我需要main返回false。 我曾经尝试过
def main():
n=len(data['dict1'])
if n:
pass
for i in range(n):
keys= access_dict(data['dict1'][i])
for k in keys:
if is_empty(data['dict1'][i][k]):
pass
if check_id(data['dict1'][i]['id']):
pass
if check_bool(data['dict1'][i]['bool_var1']):
pass
if check_bool(data['dict1'][i]['bool_var2']):
pass
if is_integer(data['dict1'][i]['var1']):
pass
if not_negative(data['dict1'][i]['var1']):
pass
if gt_zero(data['dict1'][i]['nested_dict1']):
return True
else:
return False
但这仅在gt_zero函数为false时返回false。
我的file.json看起来像这样:
{
"dict1":[
{
"id": 1,
"bool_var1": 'false',
"bool_var2": 'true',
"var1": 120,
"nested_dict1": [
{
"somevar":"some Text"
},
{
"somevar":"some Text"
},
{
"somevar":"some Text"
}
]
},
{
"id": 2,
"bool_var1": 'false',
"bool_var2": 'true',
"var1": 120,
"nested_dict1": [
{
"somevar":"some Text"
},
{
"somevar":"some Text"
},
{
"somevar":"some Text"
}
]
},
{
"id": 3,
"bool_var1": 'false',
"bool_var2": 'true',
"var1": 120,
"nested_dict1": [
{
"somevar":"some Text"
},
{
"somevar":"some Text"
},
{
"somevar":"some Text"
}
]
}
]
}
答案 0 :(得分:0)
您可以将主脚本更改为:
if not check_id(data['dict1'][i]['id']):
return False
if not check_bool(data['dict1'][i]['bool_var1']):
return False
if not check_bool(data['dict1'][i]['bool_var2']):
return False
if not is_integer(data['dict1'][i]['var1']):
return False
if not not_negative(data['dict1'][i]['var1']):
return False
if not gt_zero(data['dict1'][i]['nested_dict1']):
return False
return True