How to fill default parameters in yaml file using python -
i have yaml file content:
image_name: ubuntu components: image_name: ubuntu image_flavors: [medium, large] image_ip : 192.168.12.134 imageuser : ubuntu image_pass : ubuntu image_config: /home/ubuntu/ image_net_type: vlan image_switch_type: ovs
i have implemented script load yaml file , o/p have got in dict format.
open("test.yaml", "r") input: try: = yaml.safe_load(input) print "parsing yaml file completed" print except yaml.yamlerror error: print(error)
my dict format below:
{'image_name': 'ubuntu', 'components': {'image_ip': '192.168.12.134', 'image_pass': 'ubuntu', 'image_switch_type': 'ovs', 'imageuser': 'ubuntu', 'image_name': 'ubuntu', 'image_flavors': ['medium', 'large'], 'image_net_type': 'vlan', 'image_config': '/home/ubuntu/'}}
how can fill default parameters if key has no values?
hmm, mean this:
default_values = { 'image_name': 'some', 'components': { 'image_ip': '192.168.1.1', 'image_pass': 'default_password', 'image_switch_type': 'ovs', 'imageuser': 'default_user', 'image_name': 'default_name', 'image_flavors': ['medium', 'large'], 'image_net_type': 'vlan', 'image_config': '/default/home/' } } def setdefault_recursively(tgt, default = default_values): k in default: if isinstance(default[k], dict): # if current item dict, # expand recursively setdefault_recursively(tgt.setdefault(k, {}), default[k]) else: # ... otherwise set default value if it's not set before tgt.setdefault(k, default[k]) dic = { 'image_name': 'ubuntu', 'components': { 'image_name': 'ubuntu' } setdefault_recursively(dic)
?
this should fill dic
read yaml file default values, recursively:
{'components': {'image_config': '/default/home/', 'image_flavors': ['medium', 'large'], 'image_ip': '192.168.1.1', 'image_name': 'ubuntu', 'image_net_type': 'vlan', 'image_pass': 'default_password', 'image_switch_type': 'ovs', 'imageuser': 'default_user'}, 'image_name': 'ubuntu'}
that is, settings within structure weren't explicitly set yaml file, have received default values default_values
, uhmm, recursively.
Comments
Post a Comment