REPL : read-eval-print loop.
I created this REPL for using Code_Aster by EDF, but it can be used in anything that understands Python code (and includes tkinter
). This greatly reduced the learning curve of Code_Aster for me.
Although Code_Aster can be controlled using Python scripts, the .comm
files cannot be run directly (one must use an export
file) and it is enclosed in a virtual environments that makes interactive execution difficult.
Luckily, tkinter
is included by default in Python and this lets us display a small interface to test code. The execution is paused until the REPL is closed.
In order to access all variables in your .comm
file, the function must be defined from inside the file.
I have found two ways to achieve this:
- Copy-paste the content of
my_REPL.py
into your.comm
file. This is straightforward, but clutters your code and makes maintenance harder when you have many studies. - At the beginning of your
.comm
file, use this code to "copy-paste" the function at runtime:
with open("C:/path/to/my_REPL.py", encoding='utf-8') as f:
entire_file = f.read()
exec(entire_file)
If you know a better way, please contact me.
Examples:
my_repl() # open the REPL
my_repl("# DEBUT") # open and execute a command (a comment in this case)
my_repl(["mail = MAIL_PY()", "mail.FromAster(mesh)"]) # run several commands after opening the REPL
If the command was processed using...
eval
:µv>
is printed before the result.exec
:µX>
is printed before the result. It rarely happens since most executed commands returnNone
.- and if an error occurred:
µ Error:
.
The menu bar contains some useful snippets of code.
<Up>
: restore last command<Enter>
: execute command<Ctrl>+l
(Ctrl+L): clear previous outputs
- If the REPL is open and the export file is called again, the execution ends with EXIT_CODE=4. Simply run the
export
file again and it will work. - It is currently not possible to skip a line or move the cursor up while editing your commands.
- Only the last command is kept in history (can't press
<Up>
again to access previously run commands). - Sometimes
print
prints to theas_run
log instead of the REPL. Or it doesn't print anything. Maybe I should overwrite theprint
function withwrite_to_console
. - No syntax highlighting.
Thanks to SimulEase for making Salome-Meca and Code_Aster available on Windows.
If you use the Sublime Text text editor, this allows you to call as_run
with the active export
file by pressing Ctrl+B.
Simply put Code_Aster_as_run.sublime-build
in your User folder ("Preferences > Browse Packages... > User") and edit the file to set the correct path to your as_run.bat
.