From ec72b2626f71d91b36a4782b37dd341f4b47d037 Mon Sep 17 00:00:00 2001 From: imcovangent <I.vanGent@tudelft.nl> Date: Mon, 26 Nov 2018 15:41:25 +0100 Subject: [PATCH] Added safe handling of sleep_time as float for CMDOWS saving and loading. Former-commit-id: b80e448cdbcf0a6ab14bce00febe8091343dce08 --- examples/scripts/sellar_problem.py | 2 +- kadmos/graph/mixin_equation.py | 9 ++++----- kadmos/utilities/general.py | 9 +++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/scripts/sellar_problem.py b/examples/scripts/sellar_problem.py index ab50a3069..21d6b052c 100644 --- a/examples/scripts/sellar_problem.py +++ b/examples/scripts/sellar_problem.py @@ -50,7 +50,7 @@ rcg.add_node('D3', category='function') rcg.add_node('F1', category='function', function_type='regular', sleep_time=0.) rcg.add_node('F2', category='function') rcg.add_node('G1', category='function', function_type='regular', sleep_time=0.) -rcg.add_node('G2', category='function', function_type='regular', sleep_time=0.) +rcg.add_node('G2', category='function', function_type='regular') # All variable nodes are defined rcg.add_node('/dataSchema/settings/a', category='variable', label='a') rcg.add_node('/dataSchema/settings/c', category='variable', label='c') diff --git a/kadmos/graph/mixin_equation.py b/kadmos/graph/mixin_equation.py index 59119fdf3..4a6e7a8c6 100644 --- a/kadmos/graph/mixin_equation.py +++ b/kadmos/graph/mixin_equation.py @@ -3,8 +3,7 @@ import math import logging from ..utilities.xmls import Element -from ..utilities.general import get_uid - +from ..utilities.general import get_uid, make_float_or_keep_none # Settings for the logger logger = logging.getLogger(__name__) @@ -231,8 +230,8 @@ class EquationMixin(object): # Create mathematicalFunctions/mathematicalFunction cmdows_math_func = cmdows_math_funcs.add('mathematicalFunction', uID=graph_math_func) cmdows_math_func.add('label', self.node[graph_math_func].get('label', graph_math_func)) - cmdows_math_func.add('functionType', self.node[graph_math_func].get('function_type', graph_math_func)) - cmdows_math_func.add('sleepTime', self.node[graph_math_func].get('sleep_time', graph_math_func)) + cmdows_math_func.add('functionType', self.node[graph_math_func].get('function_type')) + cmdows_math_func.add('sleepTime', make_float_or_keep_none(self.node[graph_math_func].get('sleep_time'))) # Create mathematicalFunctions/mathematicalFunction/inputs with children cmdows_inputs = cmdows_math_func.add('inputs') @@ -279,7 +278,7 @@ class EquationMixin(object): shape='s', label=function.findtext('label'), function_type=function.findtext('functionType'), - sleep_time=float(function.findtext('sleepTime'))) + sleep_time=make_float_or_keep_none(function.findtext('sleepTime'))) for inp in function.findall('inputs/input'): self.add_edge(inp.findtext('parameterUID').replace("'", '"'), function.get('uID'), equation_label=inp.findtext('equationLabel'), diff --git a/kadmos/utilities/general.py b/kadmos/utilities/general.py index d8daa97e0..98d1a505e 100644 --- a/kadmos/utilities/general.py +++ b/kadmos/utilities/general.py @@ -858,3 +858,12 @@ def unzip_file(file_to_unzip, destination_folder=None): zip_ref.extractall(destination_folder) zip_ref.close() return extracted_file + + +def make_float_or_keep_none(value): + if isinstance(value, (float, int, str)): + return float(value) + elif value is None: + return None + else: + raise AssertionError('Could not process value {} which is of type {}.'.format(value, type(value))) \ No newline at end of file -- GitLab