SIGVerse with Python

The aim of this tutorial is to show:

  • how to import a system-define and user-define python modules to be used inside Sigverse functions written in C++
  • how to read the values returned by Python modules (e.g. double., list etc.) into C++ data types like double, arrays etc/

We saw in the previous tutorial that the "exec" function runs the arbitrary code in the string parameter within the specified namespace. We showed the following examples for that, in which we showed the use of normal, non-imported code.

Of course, we would like to import modules (both user defined and pre-designed system modules) and extract values for them.

Here, I will show one example of standard module "random". The following example is taken from the blog by Joseph Turner

Here we imported the random module by executing the corresponding Python statement within the __main__ namespace, bringing the module into the namespace. After the module is available, we can use functions, objects, and variables within the namespace. In this example, we use the "eval" function, which returns the result of the passed-in Python statement, to create a boost::python object containing a random value as returned by the random() function in the random module. Finally, we extract the value as a C++ double type and print it.

As suggested in the blog, there is a more object oriented way to do this:

In the above example, random module is imported, but this time using the boost::python import function, which loads the module into a boost Python object. Next, the random function object is extracted from the random module and stored in a boost::python object. The function is called, returning a Python object containing the random number. Finally, the double value is extracted and printed. In general, all Python objects can be handled in this way – functions, classes, built-in types.

Now, please copy the following code and name it as "ControllerSample.cpp".

The code as following functionalities

  • it executes normal, non-imported code
  • imports a system defined module "random", call its attribute functions and reads the returned values into C++ data type
  • shows an object-oriented way to import a module into Sigverse function, call its attribute functions reads the returned values into C++ data type
  • using a user-defined module called sample.py which returns a list and list into a C++ array data type
  • use of error parsing

Please set the following environment variables to set the path to boost library.

         export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/pool/lib

Now, execute "make" it will create a ControllerSample.so file.

        make

Please copy the following xml file and name it as "WorldSample.xml"

Please copy the following python file and name it as "sample.py". It imports numpy and returns a list.

Please copy the following Makefile file and name it as "Makefile"

Also set the PYTHONPATH

        export PYTHONPATH=$PYTHONPATH:/path_to_your_workspace

Now, please load the world file into Sigverse and push "Start" Button.

       ./sigverse.sh -w ./WorldSample.xml -p your_port_number 
Counter: 6370, today: 2, yesterday: 0

Front page   Edit Freeze Diff Backup Upload Copy Rename Reload   New List of pages Search Recent changes   Help   RSS of recent changes
Last-modified: 2013-12-28 (Sat) 12:54:41 (3762d)