Dune-Fufem 2.11-git
Loading...
Searching...
No Matches

Wrapper class for python modules. More...

#include <dune/fufem/python/module.hh>

Inheritance diagram for Python::Module:
Inheritance graph

Classes

class  AutoRunCodeStream
 A stream that executed all python code it is fed with on destruction. More...
 

Public Member Functions

 Module ()
 Construct empty Module.
 
 Module (PyObject *p)
 Construct Module from PyObject*.
 
 Module (const Reference &other)
 Construct Module from Reference.
 
virtual ~Module ()
 Destructor.
 
virtual Moduleoperator= (const Reference &other)
 Assignment.
 
virtual void run (const std::string &code)
 Run python code given as string.
 
AutoRunCodeStream runStream ()
 Obtain a stream to feed the code with multiple lines of python code.
 
void runFile (const std::string &fileName)
 Run python code in file given by name.
 
Reference evaluate (const std::string &expression)
 Evaluate python expression given as string.
 
Module importAs (Module module, const std::string &importedName)
 Import another module into this one.
 
Module importAs (const std::string &moduleName, const std::string &importedName)
 Import another module into this one.
 
Module import (Module module)
 Import another module into this one.
 
Module import (const std::string &moduleName)
 Import another module into this one.
 
 operator PyObject * () const
 Access to stored reference.
 
template<class T >
void toC (T &t) const
 Convert to C object.
 
template<class T >
std::enable_if< Conversion< T >::useDefaultConstructorConversion, T >::type toC () const
 Convert to C object.
 
template<class T >
std::enable_if< Conversion< T >::useCustomConstructorConversion, T >::type toC () const
 Convert to C object.
 
bool hasAttr (const std::string &name) const
 Check if object has attribute of given name.
 
Reference get (const std::string &name) const
 Query attribute of given name.
 
template<class V >
void set (const std::string &name, const V &value)
 Set attribute of given name.
 
std::string str () const
 String representation of this object.
 

Protected Member Functions

void assertPyObject (const std::string &origin) const
 Assert that internal PyObject* is not NULL and raise exception otherwise.
 

Static Protected Member Functions

static void assertModule (PyObject *p, const std::string &origin)
 Assert that internal PyObject* is not NULL and module and raise exception otherwise.
 

Protected Attributes

Reference dict_
 
PyObject * p_
 

Detailed Description

Wrapper class for python modules.

This class derives from Python::Reference and encapsulates module related functions.

Constructor & Destructor Documentation

◆ Module() [1/3]

Python::Module::Module ( )
inline

Construct empty Module.

◆ Module() [2/3]

Python::Module::Module ( PyObject *  p)
inline

Construct Module from PyObject*.

Only to be used if you want to extend the interface using the python api.

This forwards to the corresponding constructor of Reference and then checks if the python object is a module. If this is not the case an exception is thrown. As a consequence the reference count of the PyObject will be correctly decreased by ~Reference even if the exception is thrown.

But be careful to always increment the count of a borrowed reference BEFORE calling this constructor. I.e. always use

Module(Imp::inc(p))

instead of

Imp::inc(Module(p))

The latter may throw an exception and then decrease the count before it is increased.

◆ Module() [3/3]

Python::Module::Module ( const Reference other)
inline

Construct Module from Reference.

This checks if the python object is a module. If this is not the case an exception is thrown.

For implementors:

This will increment the count for the stored reference.

◆ ~Module()

virtual Python::Module::~Module ( )
inlinevirtual

Destructor.

Member Function Documentation

◆ assertModule()

static void Python::Module::assertModule ( PyObject *  p,
const std::string origin 
)
inlinestaticprotected

Assert that internal PyObject* is not NULL and module and raise exception otherwise.

Parameters
pThe PyObject* pointer that should be checked
originA string describing the origin of the error

◆ assertPyObject()

void Python::Reference::assertPyObject ( const std::string origin) const
inlineprotectedinherited

Assert that internal PyObject* is not NULL and raise exception otherwise.

Parameters
originA string describing the origin of the error

◆ evaluate()

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

Evaluate python expression given as string.

This evaluates a python expressions in the given string in the context of this module.

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

◆ get()

Reference Python::Reference::get ( const std::string name) const
inlineinherited

Query attribute of given name.

Parameters
nameName of attribute
Returns
The attributes value as Reference

For implementors:

The count for the returned Reference is incremented.

◆ hasAttr()

bool Python::Reference::hasAttr ( const std::string name) const
inlineinherited

Check if object has attribute of given name.

Parameters
nameName of attribute

◆ import() [1/2]

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

Import another module into this one.

This import the module with given moduleName under its own name into this module.

Parameters
moduleNameName of module to import
Returns
Reference to imported module

◆ import() [2/2]

Module Python::Module::import ( Module  module)
inline

Import another module into this one.

This imports the given module under its own name into this module.

Parameters
moduleThe module to import
Returns
Reference to imported module

◆ importAs() [1/2]

Module Python::Module::importAs ( const std::string moduleName,
const std::string importedName 
)
inline

Import another module into this one.

This import the module with given moduleName under given importedName into this module.

Parameters
moduleNameName of module to import
importedNameThe module will be imported under this name
Returns
Reference to imported module

◆ importAs() [2/2]

Module Python::Module::importAs ( Module  module,
const std::string importedName 
)
inline

Import another module into this one.

This import the given module under given importedName into this module.

Parameters
moduleThe module to import
importedNameThe module will be imported under this name
Returns
Reference to imported module

◆ operator PyObject *()

Python::Reference::operator PyObject * ( ) const
inlineinherited

Access to stored reference.

Return stored reference without changing its count. You can use this with all api methods that do not steal references.

Returns
A PyObject* representing a borrowed reference

◆ operator=()

virtual Module & Python::Module::operator= ( const Reference other)
inlinevirtual

Assignment.

This will checks if the python object is a module. If this is not the case an exception is thrown.

Reimplemented from Python::Reference.

◆ run()

virtual void Python::Module::run ( const std::string code)
inlinevirtual

Run python code given as string.

The code is executed in the context of this module. Hence all names are resolved or created in this module.

Parameters
codeA string containing python code

◆ runFile()

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

Run python code in file given by name.

The code is executed in the context of this module. Hence all names are resolved or created in this module.

Parameters
fileNameA string containing the file name

◆ runStream()

AutoRunCodeStream Python::Module::runStream ( )
inline

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

The code is executed in the context of this module. Hence all names are resolved or created in this module.

Returns
A stream that will execute all fed code on destruction

◆ set()

template<class V >
void Python::Reference::set ( const std::string name,
const V &  value 
)
inlineinherited

Set attribute of given name.

Parameters
nameName of attribute
valueNew value of attribute

For implementors:

This will not decrease the reference count for the Reference used as argument.

◆ str()

std::string Python::Reference::str ( ) const
inlineinherited

String representation of this object.

◆ toC() [1/3]

template<class T >
std::enable_if< Conversion< T >::useDefaultConstructorConversion, T >::type Python::Reference::toC ( ) const
inlineinherited

Convert to C object.

This does not change the reference count. This method uses the default constructor of T.

Template Parameters
TC type for conversion

◆ toC() [2/3]

template<class T >
std::enable_if< Conversion< T >::useCustomConstructorConversion, T >::type Python::Reference::toC ( ) const
inlineinherited

Convert to C object.

This does not change the reference count. This method uses a type dependent custom construct.

Template Parameters
TC type for conversion

◆ toC() [3/3]

template<class T >
void Python::Reference::toC ( T &  t) const
inlineinherited

Convert to C object.

This does not change the reference count.

Template Parameters
TC type for conversion
Parameters
tReference to store the result

Member Data Documentation

◆ dict_

Reference Python::Module::dict_
protected

◆ p_

PyObject* Python::Reference::p_
mutableprotectedinherited

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