From: Anthony Geay Date: Fri, 30 Dec 2022 16:00:02 +0000 (+0100) Subject: WIP X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=4f4fe13621581df3d3dbaec8ac59303d8f5d1b21;p=tools%2Fydefx.git WIP --- diff --git a/src/pydefx/CMakeLists.txt b/src/pydefx/CMakeLists.txt index f5a2ca3..3617457 100644 --- a/src/pydefx/CMakeLists.txt +++ b/src/pydefx/CMakeLists.txt @@ -39,5 +39,6 @@ SET(SCRIPTS ) INSTALL(FILES ${SCRIPTS} DESTINATION ${SALOME_INSTALL_PYTHON}/pydefx) +install(FILES mpmcn.py DESTINATION ${SALOME_INSTALL_PYTHON}) ADD_SUBDIRECTORY(schemas) ADD_SUBDIRECTORY(plugins) diff --git a/src/pydefx/mpmcn.py b/src/pydefx/mpmcn.py new file mode 100644 index 0000000..1232c49 --- /dev/null +++ b/src/pydefx/mpmcn.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# Copyright (C) 2022 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com + +import pydefx + +myParams = None + +class Pool: + def __init__(self): + self.myStudy = None + self.myScript = pydefx.PyScript() + self.mySample = None + + def map(self, func, iterable): + global myParams + if len(iterable) == 0: + return [] + # + import inspect + if not inspect.isfunction(func): + raise RuntimeError("Input is expected to be a function") + import importlib + fModule = importlib.import_module(func.__module__) + if fModule == "__main__": + raise RuntimeError("Input function is expected to be part of a module") + st = None + with open(fModule.__file__,"r") as ff: + st = ff.read() + fStr = func.__code__.co_name + """fStr = None + for elt in dir(fModule): + if eval(elt) == f: + fStr = elt + print(fStr) + break""" + if fStr is None: + raise RuntimeError("Impossible to locate function in the module containing it !") + fArgs = inspect.getfullargspec(func).args + pyScript = """{} +def _exec({}): + yxyx = {}({}) + return yxyx +""".format(st,", ".join(fArgs),fStr,", ".join(fArgs)) + # + self.myScript.loadString(pyScript) + self.mySample = self.myScript.CreateEmptySample() + # + #print(fArgs) + #print(iterable) + # + if not hasattr(iterable[0],"__iter__"): + iterable = [[elt] for elt in iterable] + # + #print(pyScript) + #print({k:v for k,*v in zip(fArgs,*iterable)}) + # + self.mySample.setInputValues( {k:v for k,*v in zip(fArgs,*iterable)} ) + # + self.myStudy.createNewJob(self.myScript, self.mySample, myParams) + # + self.myStudy.launch() + self.myStudy.wait() + # + return self.myStudy.getResult() + + def __enter__(self): + self.myStudy = pydefx.PyStudy() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + pass + pass + +def init(resultDirectory = "/tmp"): + """ + :param resourceName: Name of the resource matching one of the ${KERNEL_ROOT_DIR}/share/salome/resources/kernel/CatalogResources.xml + :param resultDirectory: Directory used to transfer results + """ + global myParams + myParams = pydefx.Parameters() + myParams.configureResource("localhost") + myParams.createResultDirectory(resultDirectory) + pass +