Skip to content

Commit

Permalink
feat(pySD): error is edit of param fails
Browse files Browse the repository at this point in the history
  • Loading branch information
yoctoyotta1024 committed Apr 17, 2024
1 parent 09c0c21 commit 2be8cf4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pySD/editconfigfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@
def update_param(node, param, new_value):
'''Function to recursively searches for 'param' key in YAML node
and updates it's value to with 'new_value' when found '''

is_success = False
if isinstance(node, dict):
if param in node:
node[param] = new_value # update value
is_success = True
else:
for key, val in node.items():
update_param(val, param, new_value)
elif isinstance(node, list):
for item in node:
update_param(item, param, new_value)

return is_success

def edit_config_params(filename, params2change):
''' rewrites config YAML file with key,value pairs listed in params2change updated to new values
whilst preserving original YAML file's formatting and comments etc. '''
Expand All @@ -45,7 +50,11 @@ def edit_config_params(filename, params2change):

# Update the parameters from the YAML file
for param, new_value in params2change.items():
update_param(data, param, new_value)
is_success = update_param(data, param, new_value)

if not is_success:
errmsg = param+"could not be updated to new value: "+str(new_value)
raise ValueError(errmsg)

# Overwrite the YAML file
with open(filename, 'w') as file:
Expand Down

0 comments on commit 2be8cf4

Please sign in to comment.