my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
},
"Branch Coverage": "-",
"ASIL": "ASIL-B",
"Test Environment": "AP",
"Test Configuration": "Per",
"Statement Coverage": "-",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
I want to do something like this: if the value of any key is '-' and value of ASIL is ASIL - D, to split 'Branch Coverage' key to two keys and assign them 0. Branch Coverage would become: "Branch Coverage Passed" :"0" and "Branch Coverage Covered" :"0"
I'm trying with this code, but I don't know how to add additional condition that those values become 0 only if ASIL is ASIL - D:
for index, _ in enumerate(my_dict['test_environments']):
for key, values in my_dict['test_environments'][index].items():
new_key = key.replace(" ", "_").lower()
if '-' in values:
value1 = 0
value2 = 0
my_dict['test_environments'][index]['branch_coverage' '_passed'] = value1
my_dict['test_environments'][index]['branch_coverage' '_covered'] = value2
CodePudding user response:
IIUC, you can use:
Option 1: replace old entry by new entry
for index, env in enumerate(my_dict['test_environments']):
if env['ASIL'] == 'ASIL-D':
data = {}
for key, val in env.items():
if env[key] == '-':
data[f"{key} Passed"] = 0
data[f"{key} Convered"] = 0
else:
data[key] = val
my_dict['test_environments'][index] = data.copy()
print(my_dict)
# Output
'test_environments': [{'Branch Coverage Passed': 0,
'Branch Coverage Convered': 0,
'ASIL': 'ASIL-D',
'Test Environment': 'APCOM',
'Test Configuration': 'Vehicle',
'Statement Coverage Passed': 0,
'Statement Coverage Convered': 0,
'Test Status': '36/36(100%)',
'Type of Code': 'handwritten',
'MC/DC Coverage Passed': 0,
'MC/DC Coverage Convered': 0,
'Verified SW Units': 'APCOM'}]}
Option 2: add new values to current entry
for env in my_dict['test_environments']:
if env['ASIL'] == 'ASIL-D':
data = {}
for key, val in env.items():
if env[key] == '-':
data[f"{key} Passed"] = 0
data[f"{key} Convered"] = 0
else:
data[key] = val
env.update(data)
print(my_dict)
# Output
{'camel': 'ok',
'test_environments': [{'Branch Coverage': '-',
'ASIL': 'ASIL-D',
'Test Environment': 'APCOM',
'Test Configuration': 'Vehicle',
'Statement Coverage': '-',
'Test Status': '36/36(100%)',
'Type of Code': 'handwritten',
'MC/DC Coverage': '-',
'Verified SW Units': 'APCOM',
'Branch Coverage Passed': 0,
'Branch Coverage Convered': 0,
'Statement Coverage Passed': 0,
'Statement Coverage Convered': 0,
'MC/DC Coverage Passed': 0,
'MC/DC Coverage Convered': 0}]}
CodePudding user response:
I am not sure to understand exactly what you want. If you want to replace '-' by 0, you can loop through like this :
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
for dic in my_dict['test_environments']:
for key in dic:
if dic[key] == '-':
dic[key] = 0
CodePudding user response:
for dic in dictionnary: for key in dic: if dic[key] == '-': dic[key] = 0
CodePudding user response:
This does literally what you said you want to happen (I think):
Sample data:
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
},
{
"Branch Coverage": "-",
"ASIL": "ASIL-B",
"Test Environment": "AP",
"Test Configuration": "Per",
"Statement Coverage": "-",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
Code:
import json # For printing result.
for test_env in my_dict['test_environments']:
if(test_env['ASIL'] == 'ASIL-D' and
any(['-' in value for value in test_env.values()])):
for key, value in test_env.items():
if value == '-':
test_env['branch_coverage' '_passed'] = 0
test_env['branch_coverage' '_covered'] = 0
print('Result:')
print(json.dumps(my_dict, indent=4, sort_keys=True))
Output:
Result:
{
"camel": "ok",
"test_environments": [
{
"ASIL": "ASIL-D",
"Branch Coverage": "-",
"MC/DC Coverage": "-",
"Statement Coverage": "-",
"Test Configuration": "Vehicle",
"Test Environment": "APCOM",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"Verified SW Units": "APCOM",
"branch_coverage_covered": 0,
"branch_coverage_passed": 0
},
{
"ASIL": "ASIL-B",
"Branch Coverage": "-",
"MC/DC Coverage": "-",
"Statement Coverage": "-",
"Test Configuration": "Per",
"Test Environment": "AP",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"Verified SW Units": "APCOM"
}
]
}