The following is a bare-bones example of how to run a statement in python, from within PureBasic and then retrieve the result from Python. As said, the example has been stripped down for simplicity sake until you can get familiar with the Python API.
Requirements: python3.3, obtain it here.
The python33.lib file resides within Python33/python33.lib (assuming your install directory is Python33, of course)
On Windows, also, the python33.dll file resides in your system32 directory (at least for XP)
Import "python33.lib"
Py_Initialize()
Py_Finalize()
PyRun_SimpleString( String.s )
PyImport_AddModule( String.s )
PyModule_GetDict( *PyModule.i )
PyDict_GetItemString( *PyObject.i, Key.s )
PyLong_AsLong( *PyObject.i )
EndImport
Define.i *module, *dictionary, *retval
If Py_Initialize()
PyRun_SimpleString( "retval = 10+1*10-2*16/(2-3)" ) ; let's run a basic mathematical expression, as a statement.
*module = PyImport_AddModule("__main__") ; Obtain a reference from main
If *module ; Did we get a reference to main?
*dictionary = PyModule_GetDict(*module) ; Let's get a reference of it's dictionary then.
If *dictionary
*retval = PyDict_GetItemString(*dictionary, "retval") ; Obtain a reference of retval
If *retval
Debug PyLong_AsLong(*retval) ; Debug the result from the interpreter
Debug 10+1*10-2*16/(2-3) ; Execute the same expression on native code
EndIf
EndIf
EndIf
Py_Finalize()
EndIf
The example above may look over-complicated, in fact we could get rid of most checks since we know there has to be a main and a dictionary as we already defined a variable… But, you have to write safe code and this implies always checking your pointers/handles.
At this point we haven’t defined any structures pertinent to Python nor have we set any variables within PB so that we may read them from Python itself, but that’s something we’ll discuss later.
Right now this code could serve you to execute simple scripts for your games or applications. For instance on an RPG game you could calculate a melee attack using a few fixed rules and then obtain the result on PB.
Have fun,
Cheers.