@Test public void testPythonJobReturnAllVariables()throws Exception{ PythonContextManager.deleteNonMainContexts(); String code = "c = a + b"; PythonJob job = new PythonJob("job1", code, false); PythonVariables inputs = new PythonVariables(); inputs.addInt("a", 2); inputs.addInt("b", 3); PythonVariables outputs = job.execAndReturnAllVariables(inputs); assertEquals(5L, (long)outputs.getIntValue("c")); inputs = new PythonVariables(); inputs.addFloat("a", 3.0); inputs.addFloat("b", 4.0); outputs = job.execAndReturnAllVariables(inputs); assertEquals(7.0, outputs.getFloatValue("c"), 1e-5); inputs = new PythonVariables(); inputs.addNDArray("a", Nd4j.zeros(3, 2).add(4)); inputs.addNDArray("b", Nd4j.zeros(3, 2).add(5)); outputs = job.execAndReturnAllVariables(inputs); assertEquals(Nd4j.zeros(3, 2).add(9), outputs.getNDArrayValue("c")); }
public PythonVariables execAndReturnAllVariables(PythonVariables inputs) throws Exception { try (PythonGIL gil = PythonGIL.lock()) { PythonContextManager.setContext(context); if (!setupRunMode) { return PythonExecutioner.execAndReturnAllVariables(code, inputs); } PythonExecutioner.setVariables(inputs); PythonObject inspect = Python.importModule("inspect"); PythonObject getfullargspec = inspect.attr("getfullargspec"); PythonObject argspec = getfullargspec.call(runF); PythonObject argsList = argspec.attr("args"); PythonObject runargs = Python.dict(); int argsCount = Python.len(argsList).toInt(); for (int i = 0; i < argsCount; i++) { PythonObject arg = argsList.get(i); PythonObject val = Python.globals().get(arg); if (val.isNone()) { throw new PythonException("Input value not received for run() argument: " + arg.toString()); } runargs.set(arg, val); } PythonObject outDict = runF.callWithKwargs(runargs); Python.globals().attr("update").call(outDict); inspect.del(); getfullargspec.del(); argspec.del(); runargs.del(); return PythonExecutioner.getAllVariables(); } }
PythonJob { public PythonVariables execAndReturnAllVariables(PythonVariables inputs) throws Exception { try (PythonGIL gil = PythonGIL.lock()) { PythonContextManager.setContext(context); if (!setupRunMode) { return PythonExecutioner.execAndReturnAllVariables(code, inputs); } PythonExecutioner.setVariables(inputs); PythonObject inspect = Python.importModule("inspect"); PythonObject getfullargspec = inspect.attr("getfullargspec"); PythonObject argspec = getfullargspec.call(runF); PythonObject argsList = argspec.attr("args"); PythonObject runargs = Python.dict(); int argsCount = Python.len(argsList).toInt(); for (int i = 0; i < argsCount; i++) { PythonObject arg = argsList.get(i); PythonObject val = Python.globals().get(arg); if (val.isNone()) { throw new PythonException("Input value not received for run() argument: " + arg.toString()); } runargs.set(arg, val); } PythonObject outDict = runF.callWithKwargs(runargs); Python.globals().attr("update").call(outDict); inspect.del(); getfullargspec.del(); argspec.del(); runargs.del(); return PythonExecutioner.getAllVariables(); } } }
PythonJob { public PythonVariables execAndReturnAllVariables(PythonVariables inputs) throws Exception { try (PythonGIL gil = PythonGIL.lock()) { PythonContextManager.setContext(context); if (!setupRunMode) { return PythonExecutioner.execAndReturnAllVariables(code, inputs); } PythonExecutioner.setVariables(inputs); PythonObject inspect = Python.importModule("inspect"); PythonObject getfullargspec = inspect.attr("getfullargspec"); PythonObject argspec = getfullargspec.call(runF); PythonObject argsList = argspec.attr("args"); PythonObject runargs = Python.dict(); int argsCount = Python.len(argsList).toInt(); for (int i = 0; i < argsCount; i++) { PythonObject arg = argsList.get(i); PythonObject val = Python.globals().get(arg); if (val.isNone()) { throw new PythonException("Input value not received for run() argument: " + arg.toString()); } runargs.set(arg, val); } PythonObject outDict = runF.callWithKwargs(runargs); Python.globals().attr("update").call(outDict); inspect.del(); getfullargspec.del(); argspec.del(); runargs.del(); return PythonExecutioner.getAllVariables(); } } @Builder /** * @param name Name for the python job. * @param code Python code. * @param setupRunMode If true, the python code is expected to have two methods: setup(), which takes no arguments, * and run() which takes some or no arguments. setup() method is executed once, * and the run() method is called with the inputs(if any) per transaction, and is expected to return a dictionary * mapping from output variable names (str) to output values. * If false, the full script is run on each transaction and the output variables are obtained from the global namespace * after execution. */  PythonJob(@Nonnull String name, @Nonnull String code, boolean setupRunMode); }
PythonJob { public PythonVariables execAndReturnAllVariables(PythonVariables inputs) throws Exception { try (PythonGIL gil = PythonGIL.lock()) { PythonContextManager.setContext(context); if (!setupRunMode) { return PythonExecutioner.execAndReturnAllVariables(code, inputs); } PythonExecutioner.setVariables(inputs); PythonObject inspect = Python.importModule("inspect"); PythonObject getfullargspec = inspect.attr("getfullargspec"); PythonObject argspec = getfullargspec.call(runF); PythonObject argsList = argspec.attr("args"); PythonObject runargs = Python.dict(); int argsCount = Python.len(argsList).toInt(); for (int i = 0; i < argsCount; i++) { PythonObject arg = argsList.get(i); PythonObject val = Python.globals().get(arg); if (val.isNone()) { throw new PythonException("Input value not received for run() argument: " + arg.toString()); } runargs.set(arg, val); } PythonObject outDict = runF.callWithKwargs(runargs); Python.globals().attr("update").call(outDict); inspect.del(); getfullargspec.del(); argspec.del(); runargs.del(); return PythonExecutioner.getAllVariables(); } } @Builder /** * @param name Name for the python job. * @param code Python code. * @param setupRunMode If true, the python code is expected to have two methods: setup(), which takes no arguments, * and run() which takes some or no arguments. setup() method is executed once, * and the run() method is called with the inputs(if any) per transaction, and is expected to return a dictionary * mapping from output variable names (str) to output values. * If false, the full script is run on each transaction and the output variables are obtained from the global namespace * after execution. */  PythonJob(@Nonnull String name, @Nonnull String code, boolean setupRunMode); void clearState(); void setup(); void exec(PythonVariables inputs, PythonVariables outputs); PythonVariables execAndReturnAllVariables(PythonVariables inputs); }
PythonJob { public PythonVariables execAndReturnAllVariables(PythonVariables inputs) throws Exception { try (PythonGIL gil = PythonGIL.lock()) { PythonContextManager.setContext(context); if (!setupRunMode) { return PythonExecutioner.execAndReturnAllVariables(code, inputs); } PythonExecutioner.setVariables(inputs); PythonObject inspect = Python.importModule("inspect"); PythonObject getfullargspec = inspect.attr("getfullargspec"); PythonObject argspec = getfullargspec.call(runF); PythonObject argsList = argspec.attr("args"); PythonObject runargs = Python.dict(); int argsCount = Python.len(argsList).toInt(); for (int i = 0; i < argsCount; i++) { PythonObject arg = argsList.get(i); PythonObject val = Python.globals().get(arg); if (val.isNone()) { throw new PythonException("Input value not received for run() argument: " + arg.toString()); } runargs.set(arg, val); } PythonObject outDict = runF.callWithKwargs(runargs); Python.globals().attr("update").call(outDict); inspect.del(); getfullargspec.del(); argspec.del(); runargs.del(); return PythonExecutioner.getAllVariables(); } } @Builder /** * @param name Name for the python job. * @param code Python code. * @param setupRunMode If true, the python code is expected to have two methods: setup(), which takes no arguments, * and run() which takes some or no arguments. setup() method is executed once, * and the run() method is called with the inputs(if any) per transaction, and is expected to return a dictionary * mapping from output variable names (str) to output values. * If false, the full script is run on each transaction and the output variables are obtained from the global namespace * after execution. */  PythonJob(@Nonnull String name, @Nonnull String code, boolean setupRunMode); void clearState(); void setup(); void exec(PythonVariables inputs, PythonVariables outputs); PythonVariables execAndReturnAllVariables(PythonVariables inputs);  }