Loading...
Searching...
No Matches
BALL::PyInterpreter Class Referencefinal
Python » Python extensions

`#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.

## 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 PathStrings & | getSysPath ()
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< PyKernel > | kernel_
static std::unique_ptr< PyServer > | server_
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

using BALL::PyInterpreter::PathStrings = std::vector<String>

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]

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]

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

  module | a Python module
  func | a function from the given module
  params | function arguments as key-value pairs

Returns
  true if the execution succeeded

## finalize()

void BALL::PyInterpreter::finalize | ( |  | ) |

static

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

## getErrorMessage()

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

static

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

Returns
  most recent error message

## getStartupLog()

BALL_DEPRECATED String BALL::PyInterpreter::getStartupLog | ( |  | ) |

inline static

Deprecated
  Error messages are directly printed to Log.error; Always returns an empty string.

Definition at line 79 of file pyInterpreter.h.

## getSysPath()

BALL_DEPRECATED const PathStrings & BALL::PyInterpreter::getSysPath | ( |  | ) |

inline static

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()

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()

bool BALL::PyInterpreter::isInitialized | ( |  | ) |

inline static

Determine the interpreter state.

Returns
  true if the interpreter is correctly initialized

Definition at line 61 of file pyInterpreter.h.

## isValid()

BALL_DEPRECATED bool BALL::PyInterpreter::isValid | ( |  | ) |

inline static

Deprecated
  Returns PyInterpreter::isInitialized()

Definition at line 76 of file pyInterpreter.h.

## run() [1/2]

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

static

Execute a string.

Parameters

  s | the 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]

BALL_DEPRECATED String BALL::PyInterpreter::run | ( | const String & | s,
|  | bool & | result )

static

Deprecated
  Use run(const std::string&, bool&) instead

## runFile()

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_name | the name of the program file

Deprecated
  Functionality replaced by Jupyter notebook plugin

## serverIsRunning()

bool BALL::PyInterpreter::serverIsRunning | ( |  | ) |

inline static

Indicates whether a PyServer instance is currently running.

Returns
  true if PyServer is running

Definition at line 144 of file pyInterpreter.h.

## setSysPath()

BALL_DEPRECATED void BALL::PyInterpreter::setSysPath | ( | const PathStrings & | path_strings | ) |

inline static

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()

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()

void BALL::PyInterpreter::stopServer | ( |  | ) |

static

Stops the PyServer (if running).

## Member Data Documentation

## kernel_

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

static protected

Definition at line 148 of file pyInterpreter.h.

## server_

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

static protected

Definition at line 149 of file pyInterpreter.h.

## sys_path_

PathStrings BALL::PyInterpreter::sys_path_

static protected

Definition at line 150 of file pyInterpreter.h.