example.py This is a python script, which has a multi-level dictionary.
test_dict = {'path': '/home/user/user_name',
'device1': {'IP': '10.10.10.10',
'password': 'pwd1',
'username': 'user1',
'name':'USER_DEFINED'},
'device2': {'IP': '11.11.11.11',
'password': 'pwd2',
'username': 'user2',
'name':'USER_DEFINED_TEST'}
}
I've written a separate python script to update the dictionary present in 'example.py'
. Currently, I could able to handle a first level dictionary [Ex: path and it's value], via option parser. python example.py --key path --value /home/user/user_name
. SO, now in order to handle updating the multilevel dictionary values. How do i optimize it ? Could you please help me ?
Working :
file_abs_path = "C://Test_Folder//example.py"
module = imp.load_source('test_dict', file_abs_path)
f_dict = module.test_dict
if f_key != "":
for key, value in f_dict.items():
if key == f_key:
f_dict[f_key] = f_value
parser.add_option("-k", "--key", dest="key",default="",help="")
parser.add_option("-v", "--value", dest="value",default="",help="")
f_key = options.key
f_value = options.value
if at all to update the device1 name & device2 name or it could be device1 settings name. How would i optimize it ?