walberla::python_coupling::PythonCallback Class Reference

Detailed Description

Run a Python function from C++ code.


python_coupling::PythonCallback callback ( "atEndOfTimestep" );
callback.data().exposePtr("blockStorage", blocks ); // blockStorage is available as global variable in script,
// the blockStorage can be modified from the python script
callback.data().exposeValue("someInteger", 42); // expose copy of variable to script
callback(); // run 'someFunction(data)' in script
// where data is a python dict that contains your exposed data
// Access data that was modified in script
int i = 0;
if ( callback.data().has<int>("integerSetInScript") ) {
i = callback.data().get<int>( "integerSetInScript" );

This example calls the python function "walberla_cpp.callbacks.atEndOfTimestep", which can be either set directly or using a decorator: .callback( "atEndOfTimestep" )

The result of the function call will be stored in the data dictionary with the key "returnValue".

Depending on how many parameters the python function expects it is called in different ways:

  • no parameters: the data is not passed
  • multiple parameters: the data dictionary is unpacked i.e. yourFunction( **data )

#include <PythonCallback.h>

Public Member Functions

 PythonCallback ()
 PythonCallback (const std::string &functionName)
 PythonCallback (const std::string &moduleOrFile, const std::string &functionName, const std::vector< std::string > &argv=std::vector< std::string >())
DictWrapperdata ()
const DictWrapperdata () const
bool isCallable () const
void operator() ()

Protected Attributes

const std::string functionName_
shared_ptr< DictWrapperexposedVars_
shared_ptr< DictWrappercallbackDict_

Constructor & Destructor Documentation

walberla::python_coupling::PythonCallback::PythonCallback ( )
walberla::python_coupling::PythonCallback::PythonCallback ( const std::string &  functionName)
walberla::python_coupling::PythonCallback::PythonCallback ( const std::string &  moduleOrFile,
const std::string &  functionName,
const std::vector< std::string > &  argv = std::vector<std::string>() 

Member Function Documentation

DictWrapper& walberla::python_coupling::PythonCallback::data ( )
const DictWrapper& walberla::python_coupling::PythonCallback::data ( ) const
bool walberla::python_coupling::PythonCallback::isCallable ( ) const
void walberla::python_coupling::PythonCallback::operator() ( )

Member Data Documentation

shared_ptr<DictWrapper> walberla::python_coupling::PythonCallback::callbackDict_
shared_ptr<DictWrapper> walberla::python_coupling::PythonCallback::exposedVars_
const std::string walberla::python_coupling::PythonCallback::functionName_

The documentation for this class was generated from the following files: