![]() |
Dune-Fufem 2.11-git
|
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()
| 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
-
key Identifier of the argument value Value 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
-
moduleName Name of the module to import
- Returns
- Python module of given name
◆ dict()
◆ 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
-
expression A 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]
Get item of dictionary.
- Parameters
-
dict A python dictionary k Key of item
- Returns
- The value of the item
For implementors:
This increments the reference count of the returned object.
◆ getItem() [2/2]
Get item of sequence.
- Parameters
-
seq A python sequence i Index 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
-
origin A string describing the origin of the error message A 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
-
moduleName Name 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
-
ref A 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
-
ref A 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
-
ref A 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
-
ref A python object
- Returns
- true if ref is a sequence, false otherwise
◆ iter()
Get iterator of iterable object.
- Parameters
-
seq An iterable python object
- Returns
- An iterator for the iterable object
For implementors:
This increments the reference count of the returned object.
◆ keys()
Get keys of python dictionary.
- Parameters
-
dict A python sequence
- Returns
- A list containing the keys of the dictionary
◆ main()
| Module Python::main | ( | ) |
◆ make_function()
| auto Python::make_function | ( | Callable | pyCallable | ) |
Convert Python::Callable to C-function object.
- Template Parameters
-
ResultType C++-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()
| auto Python::makeDifferentiableFunction | ( | const R &... | f | ) |
Create a callable differentiable function for given signature.
- Template Parameters
-
Signature Signature of function to create DerivativeTraits Traits template to use for construction of derivative signatures R Must derive from Python::Reference
- Parameters
-
f Callable python objects for the function and its derivatives
◆ makeFunction()
| auto Python::makeFunction | ( | const Reference & | f | ) |
Create a callable function for given signature.
- Template Parameters
-
Signature Signature of function to create
◆ makeObject()
| Reference Python::makeObject | ( | const T & | t | ) |
Create python object from C++ object.
- Template Parameters
-
T Type to convert
- Parameters
-
t A C++ object from any type.
- Returns
- New Reference to python object
◆ makeTuple() [1/2]
| Reference Python::makeTuple | ( | const Ts &... | ts | ) |
create a Python tuple
- Template Parameters
-
Ts types of tuple elements
- Parameters
-
ts tuple elements
- Returns
- a new tuple
◆ makeTuple() [2/2]
| Reference Python::makeTuple | ( | const Ts & | ... | ) |
◆ next()
Get next item from iterator.
- Parameters
-
iter An 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
-
code A 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
-
code A string containing python code module A 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
-
fileName A 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
-
fileName A string containing the file name module A 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:
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
-
module A python object referring to a module
- Returns
- A stream that will execute all fed code on destruction
◆ setItem() [1/2]
| void Python::setItem | ( | Reference & | dict, |
| const K & | k, | ||
| const V & | v | ||
| ) |
Set item of dictionary.
- Parameters
-
dict A python sequence k Key of item v New value of the item
For implementors:
This does not decrement the reference count of the value argument.
◆ setItem() [2/2]
| void Python::setItem | ( | Reference & | seq, |
| int | i, | ||
| const V & | v | ||
| ) |
Set item of sequence.
- Parameters
-
seq A python sequence i Index of item v New 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
-
s A 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()
| Reference Python::tuple | ( | const Ts &... | ts | ) |
