Skip to content

Interactive REPL + Non-interactive Scripting

username-removed-613407 requested to merge dglmoore/betse:repl into master

This is an attempt to implement a working interactive REPL and a simple scripting environment for BETSE in response to issue #2 (closed).

Scripting

As suggested by @leycec, we've created a submodule betse.script (it just seems more apropos than betse.be). The purpose of the module is the provide all of the bare necessities necessary for getting along in a scripting or interactive environment. The intended use as: import betse.script as be.

The betse.script submodule has two submodules api and runner. The former provides a convenience API for scripting and the REPL. Functions such as seed, initialize and simulate are provided to do exactly what they sound like they do... but conveniently. The latter provides the script-calling mechanism run_script which takes a single file path, or a sequence of file paths , and executes the scripts sequentially within the local environment. run_script takes several arguments that I will not detail here, but it is fairly flexible in terms of its namespace side-effects: dirty and isolated (the default) modes are provided. The run_script function can be used in the interactive REPL or baked into the core BETSE CLI.

REPL

So far we have two REPL implementations, one is ptpython-based and the other is code-based. If ptpython is present, then it is the default, with code as a fallback. The API is designed so that if we wanted to we could add a configuration option that allows the users to choose which they prefer.

Both REPLs have store their history in a history file, though because ptpython appears to have a non-readline compatible file format each REPL keeps a separate history. Hopefully they will be merged in a future commit?

The REPL provides all of the imports and API functions imported from betse.script as well as the run_script function.

Usage

The interactive environment can be started using betse repl. This subcommand takes no arguments. Script execution is only available within the interactive environment at the moment.

The user has two options for running simulations. They can use the standard BETSE CLI commands, interspersed with betse repl commands. Alternatively, the interactive environment has access to the SimRunner class and provides a handful of convenience functions that can be used to run the various simulation steps. An example is as below:

# An Interactive Use Case
$ betse repl
[betse] Welcome to <<BETSE 0.4.0 | CPython 3.5.2 | Windows 7>>.
>>> runner = seed("sample.yaml")
[betse] Seeding simulation with configuration file "sample.yaml".
...
[betse] Electrostatic pressure: True

>>> initialize(runner)
[betse] Initializing simulation with configuration file "sample.yaml".
...
[betse] Plotting animation "Efield_gj"...
<betse.science.simrunner.SimRunner object at 0x0000000004BDD278>

>>> sim, cells, p = load_init("sample.yaml")

>>> cells.cell_number
176

>>> run_script("active_info.py")
[betse] Computing active information at each cell membrane.
...
[betse] Animating local active information...

A Work In Progress

This is all a work in progress, so please let me know what you think in the comments section!

Merge request reports