Converting data from C++ to Python and vice versa
[
Front page
] [
New
|
List of pages
|
Search
|
Recent changes
|
Help
]
Start:
[[SIGVerse with Python]]
Allowing natural assignment from C++ data type to Python ...
To allow this kind of natural assignment, boost.python pr...
The aim of this tutorial is to show:
--converting C++ data type to Python data type
--converting Python data type to C++ data type
--modifying both C++ and Python data type together
For instance, assigning an STL string to a python object ...
#highlight(c){{
// Create a C++ string
std::string msg("Hello, Python");
// Assign it to a python object
boost::python::object py_msg = msg;
}}
Likewise, it is also important to be able to extract C++ ...
the extract type for this:
#highlight(c){{
boost::python::object obj = ... ;
std::string msg = boost::python::extract<std::string>(obj);
}}
Please use the following code for onAction() function by ...
#highlight(c){{
double MyController::onAction(ActionEvent &evt) {
double x=2.0;
Py_Initialize();
try{
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// Set x in python and access from C++.
py::exec("x=42", main_namespace);
std::cout << py::extract<int>(main_module.attr("x...
// Set y from C++ and access within python.
main_module.attr("y") = 100;
py::exec("print y", main_namespace);
// Access and modify x in python, then access fro...
py::exec("x += y", main_namespace);
std::cout << py::extract<int>(main_module.attr("x...
}
catch(boost::python::error_already_set const &){
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str <<...
}
return 1.0;
}
}}
Please use the world file and Makefile provided in previo...
$make
$sigserver.sh -w ./WorldSample.xml -p write_your_por...
After the world file loads into sigviewer please push "St...
42
100
142
This shows that aim of this tutorial to use boost convert...
If someone wants an advanced reference, please refer the ...
#highlight(end)
#counter
End:
[[SIGVerse with Python]]
Allowing natural assignment from C++ data type to Python ...
To allow this kind of natural assignment, boost.python pr...
The aim of this tutorial is to show:
--converting C++ data type to Python data type
--converting Python data type to C++ data type
--modifying both C++ and Python data type together
For instance, assigning an STL string to a python object ...
#highlight(c){{
// Create a C++ string
std::string msg("Hello, Python");
// Assign it to a python object
boost::python::object py_msg = msg;
}}
Likewise, it is also important to be able to extract C++ ...
the extract type for this:
#highlight(c){{
boost::python::object obj = ... ;
std::string msg = boost::python::extract<std::string>(obj);
}}
Please use the following code for onAction() function by ...
#highlight(c){{
double MyController::onAction(ActionEvent &evt) {
double x=2.0;
Py_Initialize();
try{
py::object main_module = py::import("__main__");
// load the dictionary object out of the main mod...
py::object main_namespace = main_module.attr("__d...
// run simple code within the main namespace usin...
// Set x in python and access from C++.
py::exec("x=42", main_namespace);
std::cout << py::extract<int>(main_module.attr("x...
// Set y from C++ and access within python.
main_module.attr("y") = 100;
py::exec("print y", main_namespace);
// Access and modify x in python, then access fro...
py::exec("x += y", main_namespace);
std::cout << py::extract<int>(main_module.attr("x...
}
catch(boost::python::error_already_set const &){
// Parse and output the exception
std::string perror_str = parse_python_exception();
std::cout << "Error in Python: " << perror_str <<...
}
return 1.0;
}
}}
Please use the world file and Makefile provided in previo...
$make
$sigserver.sh -w ./WorldSample.xml -p write_your_por...
After the world file loads into sigviewer please push "St...
42
100
142
This shows that aim of this tutorial to use boost convert...
If someone wants an advanced reference, please refer the ...
#highlight(end)
#counter
Page: