BALL  1.5.0
List of all members
BALL::PyInterpreter Class Referencefinal

#include <BALL/PYTHON/pyInterpreter.h>

Public Types

Type definitions
using PathStrings = std::vector< String >
 Used to encode the individual paths appended to sys.path for dynamic loading of modules. More...
 

Static Public Member Functions

Initialization
static void initialize ()
 
static void finalize ()
 
static bool isInitialized ()
 
static BALL_DEPRECATED void setSysPath (const PathStrings &path_strings)
 
static BALL_DEPRECATED const PathStringsgetSysPath ()
 
static BALL_DEPRECATED bool isValid ()
 
static BALL_DEPRECATED String getStartupLog ()
 
Execution
static std::pair< bool, std::string > run (const std::string &s)
 
static BALL_DEPRECATED String run (const String &s, bool &result)
 
static BALL_DEPRECATED String runFile (const String &filename)
 
static bool execute (const std::string &module, const std::string &func, const PyKernel::KeyValArgs &params)
 
static std::string getErrorMessage ()
 
static BALL_DEPRECATED bool execute (const QString &module, const QString &func, const QList< QPair< QString, QString > > &params)
 

Server

static std::unique_ptr< PyKernelkernel_
 
static std::unique_ptr< PyServerserver_
 
static PathStrings sys_path_
 
static void startServer ()
 
static void stopServer ()
 
static bool serverIsRunning ()
 

Detailed Description

Embedded Python interpreter. There's just one global instance of the interpreter, so all methods are static. The use of subinterpreters is not yet supported.

Definition at line 25 of file pyInterpreter.h.

Member Typedef Documentation

◆ PathStrings

Used to encode the individual paths appended to sys.path for dynamic loading of modules.

Definition at line 36 of file pyInterpreter.h.

Member Function Documentation

◆ execute() [1/2]

static BALL_DEPRECATED bool BALL::PyInterpreter::execute ( const QString &  module,
const QString &  func,
const QList< QPair< QString, QString > > &  params 
)
static
Deprecated:
use execute(const string&, const string&, const KeyValArgs&) instead

◆ execute() [2/2]

static bool BALL::PyInterpreter::execute ( const std::string &  module,
const std::string &  func,
const PyKernel::KeyValArgs params 
)
static

Calls a single function from a given module.

Parameters
modulea Python module
funca function from the given module
paramsfunction arguments as key-value pairs
Returns
true if the execution succeeded

◆ finalize()

static void BALL::PyInterpreter::finalize ( )
static

Stop the interpreter. Deallocate all memory occupied by the interpreter (by calling Py_Finalize).

◆ getErrorMessage()

static std::string BALL::PyInterpreter::getErrorMessage ( )
static

Returns the most recent error message emitted by the Python interpreter.

Returns
most recent error message

◆ getStartupLog()

static BALL_DEPRECATED String BALL::PyInterpreter::getStartupLog ( )
inlinestatic
Deprecated:
Error messages are directly printed to Log.error; Always returns an empty string.

Definition at line 79 of file pyInterpreter.h.

◆ getSysPath()

static BALL_DEPRECATED const PathStrings& BALL::PyInterpreter::getSysPath ( )
inlinestatic

Get the current (additional) paths added to sys.path

Deprecated:
Old String API; Might be re-added under a less misleading name in the future

Definition at line 73 of file pyInterpreter.h.

◆ initialize()

static void BALL::PyInterpreter::initialize ( )
static

Initialize the interpreter. Initialize the interpreter (by calling Py_Initialize) and load the modules sys, site, and BALL. A second call to initialize may be used to restart the intepreter. Upon start, the paths defined by setSysPath are added to sys.path. If your interpreter cannot load specific modules, add the location of your modules here.

◆ isInitialized()

static bool BALL::PyInterpreter::isInitialized ( )
inlinestatic

Determine the interpreter state.

Returns
true if the interpreter is correctly initialized

Definition at line 61 of file pyInterpreter.h.

◆ isValid()

static BALL_DEPRECATED bool BALL::PyInterpreter::isValid ( )
inlinestatic
Deprecated:
Returns PyInterpreter::isInitialized()

Definition at line 76 of file pyInterpreter.h.

◆ run() [1/2]

static std::pair<bool, std::string> BALL::PyInterpreter::run ( const std::string &  s)
static

Execute a string.

Parameters
sthe string to run (may contain multiple lines with correct indentation)
Returns
{ok, res} with ok indicating whether the execution was successful and res representing the output of the interpreter (may also contain error messages)

◆ run() [2/2]

static BALL_DEPRECATED String BALL::PyInterpreter::run ( const String s,
bool result 
)
static
Deprecated:
Use run(const std::string&, bool&) instead

◆ runFile()

static BALL_DEPRECATED String BALL::PyInterpreter::runFile ( const String filename)
static

Run a Python program from a file. If the file does not exist, or cannot be opened, an Exception::FileNotFound is thrown

Parameters
file_namethe name of the program file
Deprecated:
Functionality replaced by Jupyter notebook plugin

◆ serverIsRunning()

static bool BALL::PyInterpreter::serverIsRunning ( )
inlinestatic

Indicates whether a PyServer instance is currently running.

Returns
true if PyServer is running

Definition at line 144 of file pyInterpreter.h.

◆ setSysPath()

static BALL_DEPRECATED void BALL::PyInterpreter::setSysPath ( const PathStrings path_strings)
inlinestatic

Append additional search paths to sys.path upon initialization

Deprecated:
Old String API; Might be re-added under a less misleading name in the future

Definition at line 67 of file pyInterpreter.h.

◆ startServer()

static void BALL::PyInterpreter::startServer ( )
static

Start a PyServer instance (if not already running) to allow remote execution of Python code, e.g., via the BALLView Jupyter kernel.

◆ stopServer()

static void BALL::PyInterpreter::stopServer ( )
static

Stops the PyServer (if running).

Member Data Documentation

◆ kernel_

std::unique_ptr<PyKernel> BALL::PyInterpreter::kernel_
staticprotected

Definition at line 148 of file pyInterpreter.h.

◆ server_

std::unique_ptr<PyServer> BALL::PyInterpreter::server_
staticprotected

Definition at line 149 of file pyInterpreter.h.

◆ sys_path_

PathStrings BALL::PyInterpreter::sys_path_
staticprotected

Definition at line 150 of file pyInterpreter.h.