Description
Embedded Python code can be paused and continued in another block, which allows to access previously defined symbols and saves multiple initializations. And even after the end of an embedded code block, the embedded Pyton library does not get unloaded. Contributor: Lutz Westermann, February 2019
Small Model of Type : GAMS
Category : GAMS Test library
Main file : embpy04.gms
$title 'Test continuation of embedded code blocks' (EMBPY04,SEQ=797)
$onText
Embedded Python code can be paused and continued in another block, which
allows to access previously defined symbols and saves multiple initializations.
And even after the end of an embedded code block, the embedded Pyton library
does not get unloaded.
Contributor: Lutz Westermann, February 2019
$offText
$log --- Using Python library %sysEnv.GMSPYTHONLIB%
Scalar x /0/;
$onMultiR
$onEmbeddedCode Python:
x = 2
gams.set("x",[x])
$offEmbeddedCode x
$ifE x<>2 $abort Unexpected value in line %system.line%
* Symbol x set above should still be available
* Moving from one compile time block to another
$onEmbeddedCode Python:
x = x*2
gams.set("x",[x])
$offEmbeddedCode x
$ifE x<>4 $abort Unexpected value in line %system.line%
* Symbol x set above should still be available
* Moving from a compile time block to a execution time block
EmbeddedCode Python:
x = x*2
gams.set("x",[x])
pauseEmbeddedCode x
abort$(x<>8) 'Unexpected value in line %system.line%';
* Symbol x set above should still be available
* Pausing an execution time block and continuing a execution time block
continueEmbeddedCode:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>16) 'Unexpected value in line %system.line%';
* Symbol x set above should still be available
* Ending an execution time block and starting a new execution time block
EmbeddedCode Python:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
abort$(x<>32) 'Unexpected value in line %system.line%';
*The following is expected to fail - we cannot continue, if there is no paused EC
$onEcho > test.gms
Scalar x;
EmbeddedCode Python:
x = 4*2
gams.set("x",[x])
pauseEmbeddedCode x
scalar handle;
handle = embeddedHandle;
continueEmbeddedCode:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
continueEmbeddedCode handle:
x = x*2
gams.set("x",[x])
endEmbeddedCode x
$offEcho
$call gams test.gms lo=%GAMS.lo%
$ifE errorLevel=0 $abort Expected problem in line %system.line%