-
Notifications
You must be signed in to change notification settings - Fork 1
How to send more data to python
Elias Judin edited this page Jun 6, 2021
·
11 revisions
- To see these techniques in use view Strategy.cc. the pyFunction.
- the first command that must be run before using or calling python Scripts or PyObjects is Py_Initialize(). this function basically creates a python interpreter to run python commands and files
- to run a python command use the following command PyRun_SimpleString("import os"). in this example here we are importing the os library to interpreter.
- this is an example on how you can call on a python file. to check if it has imported you can put pModule in an if statement
//calling on myFile.py
PyObject * pName = PyUnicode_FromString("myFile");
PyObject * pModule = PyImport_Import(pName);
if(pModule)
{
// running the method myFunc in myFile.py
PyObject * pFunc = PyObject_GetAttrString(pModule, "myFunc");
if(pFunc && PyCallable_Check(pFunc))
{
//here we call on the python function and store the output in a PyObject in this example we are sending in a list so we attach an O before world_data
PyObject * pReturn = PyObject_CallFunction(pFunc,"O",world_data);
//here we convert the pyobject to the relevent c++ format
int x = (int) PyLong_AsLong(pReturn);
return x;
}
else
{
cout << "ERROR: function selectSkill()\n"<<endl;
return -1;
}
}
else
{
cout << "ERROR: Module not imported"<<endl;
return -2;
}for info on how to convert c++ to PyObject look here and how to convert from PyObject to c++ you can look here
- an example of converting a c++ data type to a python Object
//converting an integer to a PyObject
int x = 5
PyObject * temp;
temp = Py_BuildValue("i",x);- an example of extracting data from a PyObject
PyObject * objectPy;
//update objectPy
int x = (int) PyLong_AsLong(objectPy);
### Handling Python Lists
* how to define a python list
```C++
int n = 5;
PyObject* pyList = PyList_New(n);- how to get an item from the pyList (get the item at index 0)
PyObject* item = PyList_GetItem(pyList, 0);- how to set an item in a python list
int value = 5;
PyObject * item = Py_BuildValue("i",value);
PyList_SetItem(PyList,0,item);