From dfa36987389cbc67d5c6693e467ed6db05d427d8 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Thu, 15 Nov 2018 21:11:03 +0100 Subject: [PATCH] Superseeding pickle transmission to get portable strings --- doc/en/ref_operator_requirements.rst | 6 ++-- doc/fr/ref_operator_requirements.rst | 6 ++-- resources/ADAOSchemaCatalog.xml | 21 +++++++----- .../daYacsIntegration/daOptimizerLoop.py | 33 +++++++------------ src/daSalome/daYacsSchemaCreator/methods.py | 24 +++++++++----- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/doc/en/ref_operator_requirements.rst b/doc/en/ref_operator_requirements.rst index 1c4b4bd..ec883f5 100644 --- a/doc/en/ref_operator_requirements.rst +++ b/doc/en/ref_operator_requirements.rst @@ -202,12 +202,14 @@ template for the switch. It requires an external script or code named here Here is the switch template:: import Physical_simulation_functions - import numpy, logging + import numpy, logging, codecs, pickle + def loads( data ): + return pickle.loads(codecs.decode(data.encode(), "base64")) # method = "" for param in computation["specificParameters"]: if param["name"] == "method": - method = param["value"] + method = loads(param["value"]) if method not in ["Direct", "Tangent", "Adjoint"]: raise ValueError("No valid computation method is given") logging.info("Found method is \'%s\'"%method) diff --git a/doc/fr/ref_operator_requirements.rst b/doc/fr/ref_operator_requirements.rst index 12d109d..3190f3d 100644 --- a/doc/fr/ref_operator_requirements.rst +++ b/doc/fr/ref_operator_requirements.rst @@ -211,12 +211,14 @@ contenant trois fonctions nommées "*DirectOperator*", "*TangentOperator*" et "*AdjointOperator*" comme précédemment. Voici le squelette d'aiguillage:: import Physical_simulation_functions - import numpy, logging + import numpy, logging, codecs, pickle + def loads( data ): + return pickle.loads(codecs.decode(data.encode(), "base64")) # method = "" for param in computation["specificParameters"]: if param["name"] == "method": - method = param["value"] + method = loads(param["value"]) if method not in ["Direct", "Tangent", "Adjoint"]: raise ValueError("No valid computation method is given") logging.info("Found method is \'%s\'"%method) diff --git a/resources/ADAOSchemaCatalog.xml b/resources/ADAOSchemaCatalog.xml index 3abb570..af25ccf 100644 --- a/resources/ADAOSchemaCatalog.xml +++ b/resources/ADAOSchemaCatalog.xml @@ -33,7 +33,7 @@ - + @@ -536,14 +536,17 @@ if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index - @@ -552,17 +555,19 @@ logging.debug(" switching to value : "+str(switch_value)) - diff --git a/src/daSalome/daYacsIntegration/daOptimizerLoop.py b/src/daSalome/daYacsIntegration/daOptimizerLoop.py index 0a05918..ff1df91 100644 --- a/src/daSalome/daYacsIntegration/daOptimizerLoop.py +++ b/src/daSalome/daYacsIntegration/daOptimizerLoop.py @@ -32,11 +32,17 @@ import numpy import threading import sys import traceback +import codecs # Pour disposer des classes dans l'espace de nommage lors du pickle from daCore.AssimilationStudy import AssimilationStudy from daYacsIntegration import daStudy +def dumps( data ): + return str(codecs.encode(pickle.dumps(data), "base64").decode()) +def loads( data ): + return pickle.loads(codecs.decode(data.encode(), "base64")) + class OptimizerHooks: def __init__(self, optim_algo, switch_value=-1): @@ -60,27 +66,18 @@ class OptimizerHooks: specificParameters = pilot.SequenceAny_New(self.optim_algo.runtime.getTypeCode("SALOME_TYPES/Parameter")) method_name = pilot.StructAny_New(self.optim_algo.runtime.getTypeCode('SALOME_TYPES/Parameter')) method_name.setEltAtRank("name", "method") - if sys.version_info.major < 3: - method_name.setEltAtRank("value", method) - else: - method_name.setEltAtRank("value", pickle.dumps(method)) + method_name.setEltAtRank("value", dumps(method)) specificParameters.pushBack(method_name) # print self.optim_algo.has_observer if self.optim_algo.has_observer: obs_switch = pilot.StructAny_New(self.optim_algo.runtime.getTypeCode('SALOME_TYPES/Parameter')) obs_switch.setEltAtRank("name", "switch_value") - if sys.version_info.major < 3: - obs_switch.setEltAtRank("value", "1") - else: - obs_switch.setEltAtRank("value", pickle.dumps("1")) + obs_switch.setEltAtRank("value", dumps("1")) specificParameters.pushBack(obs_switch) if self.optim_algo.has_evolution_model: obs_switch = pilot.StructAny_New(self.optim_algo.runtime.getTypeCode('SALOME_TYPES/Parameter')) obs_switch.setEltAtRank("name", "switch_value") - if sys.version_info.major < 3: - obs_switch.setEltAtRank("value", self.switch_value) - else: - obs_switch.setEltAtRank("value", pickle.dumps(self.switch_value)) + obs_switch.setEltAtRank("value", dumps(self.switch_value)) specificParameters.pushBack(obs_switch) sample.setEltAtRank("specificParameters", specificParameters) @@ -400,10 +397,7 @@ class AssimilationAlgorithm_asynch(SALOMERuntime.OptimizerAlgASync): # Switch Value obs_switch = pilot.StructAny_New(self.runtime.getTypeCode('SALOME_TYPES/Parameter')) obs_switch.setEltAtRank("name", "switch_value") - if sys.version_info.major < 3: - obs_switch.setEltAtRank("value", self.da_study.observers_dict[info]["number"]) - else: - obs_switch.setEltAtRank("value", pickle.dumps(self.da_study.observers_dict[info]["number"])) + obs_switch.setEltAtRank("value", dumps(self.da_study.observers_dict[info]["number"])) specificParameters.pushBack(obs_switch) # Var @@ -413,7 +407,7 @@ class AssimilationAlgorithm_asynch(SALOMERuntime.OptimizerAlgASync): # Remove Data Observer, so you can ... var.removeDataObserver(self.obs) # Pickle then ... - var_struct.setEltAtRank("value", pickle.dumps(var)) + var_struct.setEltAtRank("value", dumps(var)) specificParameters.pushBack(var_struct) # Add Again Data Observer if self.da_study.observers_dict[info]["scheduler"] != "": @@ -424,10 +418,7 @@ class AssimilationAlgorithm_asynch(SALOMERuntime.OptimizerAlgASync): # Info info_struct = pilot.StructAny_New(self.runtime.getTypeCode('SALOME_TYPES/Parameter')) info_struct.setEltAtRank("name", "info") - if sys.version_info.major < 3: - info_struct.setEltAtRank("value", self.da_study.observers_dict[info]["info"]) - else: - info_struct.setEltAtRank("value", pickle.dumps(self.da_study.observers_dict[info]["info"])) + info_struct.setEltAtRank("value", dumps(self.da_study.observers_dict[info]["info"])) specificParameters.pushBack(info_struct) sample.setEltAtRank("specificParameters", specificParameters) diff --git a/src/daSalome/daYacsSchemaCreator/methods.py b/src/daSalome/daYacsSchemaCreator/methods.py index 428440d..db4e0c1 100644 --- a/src/daSalome/daYacsSchemaCreator/methods.py +++ b/src/daSalome/daYacsSchemaCreator/methods.py @@ -455,7 +455,9 @@ def create_yacs_proc(study_config): except: raise ValueError("Exception in opening function script file: " + script_filename) node_script = "#-*- coding: utf-8 -*-\n" - node_script += "import sys, os, numpy, logging\n" + node_script += "import sys, os, numpy, logging, pickle, codecs\n" + node_script += "def loads( data ):\n" + node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n" node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n" node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n" node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n" @@ -465,7 +467,7 @@ def create_yacs_proc(study_config): node_script += """# ==============================================\n""" node_script += """__method = None\n""" node_script += """for param in computation["specificParameters"]:\n""" - node_script += """ if param["name"] == "method": __method = param["value"]\n""" + node_script += """ if param["name"] == "method": __method = loads(param["value"])\n""" node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n""" node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n""" node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n""" @@ -538,7 +540,9 @@ def create_yacs_proc(study_config): except: raise ValueError("Exception in opening function script file: " + script_filename) node_script = "#-*- coding: utf-8 -*-\n" - node_script += "import sys, os, numpy, logging\n" + node_script += "import sys, os, numpy, logging, pickle, codecs\n" + node_script += "def loads( data ):\n" + node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n" node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n" node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n" node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n" @@ -548,7 +552,7 @@ def create_yacs_proc(study_config): node_script += """# ==============================================\n""" node_script += """__method = None\n""" node_script += """for param in computation["specificParameters"]:\n""" - node_script += """ if param["name"] == "method": __method = param["value"]\n""" + node_script += """ if param["name"] == "method": __method = loads(param["value"])\n""" node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n""" node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n""" node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n""" @@ -657,7 +661,9 @@ def create_yacs_proc(study_config): except: raise ValueError("Exception in opening function script file: " + script_filename) node_script = "#-*- coding: utf-8 -*-\n" - node_script += "import sys, os, numpy, logging\n" + node_script += "import sys, os, numpy, logging, pickle, codecs\n" + node_script += "def loads( data ):\n" + node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n" node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n" node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n" node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n" @@ -666,7 +672,7 @@ def create_yacs_proc(study_config): node_script += """# ==============================================\n""" node_script += """__method = None\n""" node_script += """for param in computation["specificParameters"]:\n""" - node_script += """ if param["name"] == "method": __method = param["value"]\n""" + node_script += """ if param["name"] == "method": __method = loads(param["value"])\n""" node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n""" node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n""" node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n""" @@ -739,7 +745,9 @@ def create_yacs_proc(study_config): except: raise ValueError("Exception in opening function script file: " + script_filename) node_script = "#-*- coding: utf-8 -*-\n" - node_script += "import sys, os, numpy, logging\n" + node_script += "import sys, os, numpy, logging, pickle, codecs\n" + node_script += "def loads( data ):\n" + node_script += " return pickle.loads(codecs.decode(data.encode(), 'base64'))\n" node_script += "filepath = \"" + os.path.dirname(script_filename) + "\"\n" node_script += "filename = \"" + os.path.basename(script_filename) + "\"\n" node_script += "if sys.path.count(filepath)==0 or (sys.path.count(filepath)>0 and sys.path.index(filepath)>0):\n" @@ -748,7 +756,7 @@ def create_yacs_proc(study_config): node_script += """# ==============================================\n""" node_script += """__method = None\n""" node_script += """for param in computation["specificParameters"]:\n""" - node_script += """ if param["name"] == "method": __method = param["value"]\n""" + node_script += """ if param["name"] == "method": __method = loads(param["value"])\n""" node_script += """if __method not in ["Direct", "Tangent", "Adjoint"]:\n""" node_script += """ raise ValueError("ComputationFunctionNode: no valid computation method is given, it has to be Direct, Tangent or Adjoint (\'%s\' given)."%__method)\n""" node_script += """logging.debug("ComputationFunctionNode: Found method is \'%s\'"%__method)\n""" -- 2.39.2