Dune-Fufem 2.11-git
Loading...
Searching...
No Matches
Python Namespace Reference

Namespaces

namespace  Literals
 

Classes

class  Callable
 Wrapper class for callable python objects. More...
 
struct  Conversion
 Conversion of C type T from and to PyObject*. More...
 
class  Module
 Wrapper class for python modules. More...
 
class  Reference
 Wrapper class for python objects. More...
 

Functions

void handlePythonError (const std::string &origin, const std::string &message)
 If a python error occurred throw an exception and clear python error indicator.
 
Reference dict ()
 Create an empty Python tuple.
 
template<class Key , class Value >
auto arg (Key key, Value &&value)
 Create a keyword argument.
 
template<class ResultType >
auto make_function (Callable pyCallable)
 Convert Python::Callable to C-function object.
 
bool isModule (const Reference &ref)
 Check if python object is a module.
 
void start ()
 Start embedded python interpreter.
 
void stop ()
 Stop embedded python interpreter.
 
Module import (const std::string &moduleName)
 Import python module given by name.
 
Module createModule (const std::string &moduleName)
 Create python module given by name.
 
Module main ()
 Obtain the main module.
 
void run (const std::string &code)
 Run python code given as string.
 
void run (const std::string &code, Reference &module)
 Run python code given as string in specified module.
 
Module::AutoRunCodeStream runStream ()
 Obtain a stream to feed the code with multiple lines of python code.
 
Module::AutoRunCodeStream runStream (Module module)
 Obtain a stream to feed the code with multiple lines of python code for execution in specified module.
 
void runFile (const std::string &fileName)
 Run python code in file given by name.
 
void runFile (const std::string &fileName, Module module)
 Run python code in file given by name in specified module.
 
Reference evaluate (const std::string &expression)
 Evaluate python expression given as string.
 
Reference evaluate (const std::string &expression, Module module)
 
template<class T >
Reference makeObject (const T &t)
 Create python object from C++ object.
 
template<typename... Ts>
Reference makeTuple (const Ts &... ts)
 create a Python tuple
 
template<typename... Ts>
Reference tuple (const Ts &... ts)
 
int size (const Reference &s)
 Get size of a python sequence.
 
Reference iter (const Reference &seq)
 Get iterator of iterable object.
 
Reference next (const Reference &iter)
 Get next item from iterator.
 
Reference getItem (const Reference &seq, int i)
 Get item of sequence.
 
Reference getItem (const Reference &dict, const Reference &k)
 Get item of dictionary.
 
template<class V >
void setItem (Reference &seq, int i, const V &v)
 Set item of sequence.
 
template<class K , class V >
void setItem (Reference &dict, const K &k, const V &v)
 Set item of dictionary.
 
bool isSequence (const Reference &ref)
 Check if python object is some sequence.
 
bool isDict (const Reference &ref)
 Check if python object is a dictionary.
 
bool isCallable (const Reference &ref)
 Check if python object is callable.
 
Reference keys (const Reference &dict)
 Get keys of python dictionary.
 
template<class Signature , template< class > class DerivativeTraits = Dune::Functions::DefaultDerivativeTraits>
auto makeFunction (const Reference &f)
 Create a callable function for given signature.
 
template<class Signature , template< class > class DerivativeTraits = Dune::Functions::DefaultDerivativeTraits, class... R>
auto makeDifferentiableFunction (const R &... f)
 Create a callable differentiable function for given signature.
 
template<typename... Ts>
Reference makeTuple (const Ts &...)
 

Function Documentation

◆ arg()

template<class Key , class Value >
auto Python::arg ( Key  key,
Value &&  value 
)

Create a keyword argument.

While the value may be stored as reference or value, if an l-value or r-value reference is passed, the key is always stored by value. As a special case, when passing a const char* it will be converted to a std::string.

Parameters
keyIdentifier of the argument
valueValue of the argument

◆ createModule()

Module Python::createModule ( const std::string moduleName)

Create python module given by name.

Create a new python module with given name. If a module with the given name was already imported, then this module is returned.

Otherwise a new empty module is created and returned, even if a module with the given name is in the search path.

Parameters
moduleNameName of the module to import
Returns
Python module of given name

◆ dict()

Reference Python::dict ( )

Create an empty Python tuple.

Entries can be inserted using setItem()

◆ evaluate() [1/2]

Reference Python::evaluate ( const std::string expression)

Evaluate python expression given as string.

This evaluates a python expressions in the given string in the main namespace.

Parameters
expressionA string containing a python expression
Returns
Result of expression evaluation

◆ evaluate() [2/2]

Reference Python::evaluate ( const std::string expression,
Module  module 
)

◆ getItem() [1/2]

Reference Python::getItem ( const Reference dict,
const Reference k 
)

Get item of dictionary.

Parameters
dictA python dictionary
kKey of item
Returns
The value of the item

For implementors:

This increments the reference count of the returned object.

◆ getItem() [2/2]

Reference Python::getItem ( const Reference seq,
int  i 
)

Get item of sequence.

Parameters
seqA python sequence
iIndex of item
Returns
The i-th item of s

For implementors:

This increments the reference count of the returned object.

◆ handlePythonError()

void Python::handlePythonError ( const std::string origin,
const std::string message 
)

If a python error occurred throw an exception and clear python error indicator.

Parameters
originA string describing the origin of the error
messageA string used as error message

◆ import()

Module Python::import ( const std::string moduleName)

Import python module given by name.

The first time you call the import() method the module is actually imported and the contained initialization code is executed. Later calls will only return a reference to the already imported module without executing the initialization code again.

Parameters
moduleNameName of the module to import
Returns
Python module of given name

◆ isCallable()

bool Python::isCallable ( const Reference ref)

Check if python object is callable.

Parameters
refA python object
Returns
true if ref is callable, false otherwise

◆ isDict()

bool Python::isDict ( const Reference ref)

Check if python object is a dictionary.

Parameters
refA python object
Returns
true if ref is a dictionary, false otherwise

◆ isModule()

bool Python::isModule ( const Reference ref)

Check if python object is a module.

Parameters
refA python object
Returns
true if ref is a module, false otherwise

◆ isSequence()

bool Python::isSequence ( const Reference ref)

Check if python object is some sequence.

Parameters
refA python object
Returns
true if ref is a sequence, false otherwise

◆ iter()

Reference Python::iter ( const Reference seq)

Get iterator of iterable object.

Parameters
seqAn iterable python object
Returns
An iterator for the iterable object

For implementors:

This increments the reference count of the returned object.

◆ keys()

Reference Python::keys ( const Reference dict)

Get keys of python dictionary.

Parameters
dictA python sequence
Returns
A list containing the keys of the dictionary

◆ main()

Module Python::main ( )

Obtain the main module.

This is a shortcut for import("__main__")

Returns
Python main module

◆ make_function()

template<class ResultType >
auto Python::make_function ( Callable  pyCallable)

Convert Python::Callable to C-function object.

Template Parameters
ResultTypeC++-type of result

This will convert the callable python object to a function object with desired result type. The difference between the passed and returned function object is, that the latter converts return values to the desired C++ type whereas the former returns a Python::Reference.

◆ makeDifferentiableFunction()

template<class Signature , template< class > class DerivativeTraits = Dune::Functions::DefaultDerivativeTraits, class... R>
auto Python::makeDifferentiableFunction ( const R &...  f)

Create a callable differentiable function for given signature.

Template Parameters
SignatureSignature of function to create
DerivativeTraitsTraits template to use for construction of derivative signatures
RMust derive from Python::Reference
Parameters
fCallable python objects for the function and its derivatives

◆ makeFunction()

template<class Signature , template< class > class DerivativeTraits = Dune::Functions::DefaultDerivativeTraits>
auto Python::makeFunction ( const Reference f)

Create a callable function for given signature.

Template Parameters
SignatureSignature of function to create

◆ makeObject()

template<class T >
Reference Python::makeObject ( const T &  t)

Create python object from C++ object.

Template Parameters
TType to convert
Parameters
tA C++ object from any type.
Returns
New Reference to python object

◆ makeTuple() [1/2]

template<typename... Ts>
Reference Python::makeTuple ( const Ts &...  ts)

create a Python tuple

Template Parameters
Tstypes of tuple elements
Parameters
tstuple elements
Returns
a new tuple

◆ makeTuple() [2/2]

template<typename... Ts>
Reference Python::makeTuple ( const Ts &  ...)

◆ next()

Reference Python::next ( const Reference iter)

Get next item from iterator.

Parameters
iterAn iterator object
Returns
The next object the iterator points to

For implementors:

This increments the reference count of the returned object.

◆ run() [1/2]

void Python::run ( const std::string code)

Run python code given as string.

This executes the python code in the given string in the main module.

Although you can also execute several lines using the run() method by separated them with '
', it will be much more comfortable using the runStream() method.

Parameters
codeA string containing python code

◆ run() [2/2]

void Python::run ( const std::string code,
Reference module 
)

Run python code given as string in specified module.

This is the same as run(code) except that the code is executed in the context of the given module. Hence all names are resolved or created in this module.

Parameters
codeA string containing python code
moduleA python object referring to a module

◆ runFile() [1/2]

void Python::runFile ( const std::string fileName)

Run python code in file given by name.

This executes the python code in the given file in the main namespace.

Parameters
fileNameA string containing the file name

◆ runFile() [2/2]

void Python::runFile ( const std::string fileName,
Module  module 
)

Run python code in file given by name in specified module.

This is the same as runFile(fileName) except that the code is executed in the context of the given module. Hence all names are resolved or created in this module.

Parameters
fileNameA string containing the file name
moduleA python object referring to a module

◆ runStream() [1/2]

Module::AutoRunCodeStream Python::runStream ( )

Obtain a stream to feed the code with multiple lines of python code.

This returns a AutoRunCodeStream object that can be fed with multiple lines of python code. Once the stream is destroyed the code is executed. This function allows to execute multiline python code in a more comfortable way:

<< std::endl << "def add(x,y):"
<< std::endl << " return x+y";
Module::AutoRunCodeStream runStream()
Obtain a stream to feed the code with multiple lines of python code.
Definition common.hh:270
T endl(T... args)

This could not be done by multiple Python::run() statements since you have to do all function/class definitions at once.

This executes the python code in the given string in the main namespace.

Returns
A stream that will execute all fed code on destruction

◆ runStream() [2/2]

Module::AutoRunCodeStream Python::runStream ( Module  module)

Obtain a stream to feed the code with multiple lines of python code for execution in specified module.

This is the same as runStream() except that the code is executed in the context of the given module. Hence all names are resolved or created in this module.

Parameters
moduleA python object referring to a module
Returns
A stream that will execute all fed code on destruction

◆ setItem() [1/2]

template<class K , class V >
void Python::setItem ( Reference dict,
const K &  k,
const V &  v 
)

Set item of dictionary.

Parameters
dictA python sequence
kKey of item
vNew value of the item

For implementors:

This does not decrement the reference count of the value argument.

◆ setItem() [2/2]

template<class V >
void Python::setItem ( Reference seq,
int  i,
const V &  v 
)

Set item of sequence.

Parameters
seqA python sequence
iIndex of item
vNew value of i-th item

For implementors:

This does not decrement the reference count of the value argument.

◆ size()

int Python::size ( const Reference s)

Get size of a python sequence.

Parameters
sA python sequence
Returns
Size of s

◆ start()

void Python::start ( )

Start embedded python interpreter.

This must be called before any other python action.

◆ stop()

void Python::stop ( )

Stop embedded python interpreter.

This must be called after any other python action before the program terminates.

◆ tuple()

template<typename... Ts>
Reference Python::tuple ( const Ts &...  ts)