Python script

xiaoxiao2021-03-05  23

Python script

Boost supports Python is really simple. It is much more useful than Lua. The most troublesome problem is the debugging of scripts. It is more painful: (, but also a DLL, commissioning directly in Python, but that, some The situation is more troublesome.

As for example, in fact, everyone can go to CODESAMPLER to find, very simple, have time I put a post on my package to take a look :)

Www.codesampler.com

Some places may not be able to connect, the following is a code of example, you can refer to it.

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: py_embedded_python.cpp // Author: Kevin Harris (Kevin@codesampler.com) / / Last Modified: 05/11/04 // Description: This sample demonstrates how to use the Boost.Python library // to embed Python into a C application This will allow the // application to execute Python scripts The sample also /.. / DemonStrates How To setup Special C Functions Which Will // Allow C and Python to Communicate Back and Forth, // incruding how to setup a call-back function.//// NOTE: /// Besides Python 2.3, you ' Ll Need The Boost Libraries to Compile this Project: // http://www.boost.org//// More information on boost.python can be found here: // http://www.boost.org/libs /Python/doc/index.html//----------------------------------------------------------------------- -------------------------------------

#include #include using namespace std;

#include Using namespace boost :: python;

/ / -------------------------------------------------------------------------------------------- ---------------------------- // Create A C Function Will Allow Python to Get An Int Value from the // Application. / / --------------------------------------------------- ----------------------------

INT g_somevalue = 55;

PyObject * getValue (Pyobject * Self, PyObject * args) {RETURN PY_BUILDVALUE ("I", g_somevalue);} // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- --- // Create a C Function Will Allow Python to set or pass four floats to // the application.//------------------------ -------------------------------------------------- ---

PyObject * setxyzw (pyobject * self, pyobject * args) {float x, y, z, w;

IF (Pyarg_Parsetuple (Args, "FFFF", & X, & Y, & Z, & W) {cout << "Script called setxyzw and passed:" << "x =" << x << "y =" << Y < <"Z =" << Z << "w =" << w << endl;}

PY_INCREF (py_none); return py_none;}

/ / -------------------------------------------------------------------------------------------- ----------------------------- // Define a Function Which Will Allow a Python Script to set a call-back for /// Application to use.//-------------------------------------------- ---------------------------------

Static PyObject * g_pythoncallback = null;

Static PyObject * setCallback (pyobject * self, pyobject * args) {pyObject * result = null; pyobject * temp = null;

IF (Pyarg_Parsetuple (ARGS, "O", & Temp)) {if (! pycallable_check (temp)) {pyerr_setstring ("Parameter Must Be Callable"); PY_INCREF (PY_NONE); RETURN PY_NONE;}

Py_XINCREF (temp); // Ref the new call-back Py_XDECREF (g_pythonCallback); // Unref the previous call-back g_pythonCallback = temp; // Cache the new call-back} Py_INCREF (Py_None); return Py_None;}

/ / -------------------------------------------------------------------------------------------- ----------------------------- // make Python Aware of Our Special C functions above.//------- -------------------------------------------------- --------------------

PyMethodDef moduleFuncs [] = {{ "getvalue", getvalue, METH_VARARGS, "Returns an int value"}, { "setXYZW", setXYZW, METH_VARARGS, "Sets a 4 component vector"}, { "setCallback", setCallback, METH_VARARGS, "Sets a call-back function in python"}}}};

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: readpythonscript () // desc: // -------- -------------------------------------------------- ------------------- CHAR * Readpythonscript (const char * filename) {file * pfile = fopen (filename, "r");

IF (pfile == null) {cout << "cannot open python script file, /" << filename << "/"! "<< endl; return 0;}

Struct _stat fileestats;

IF (_stat (filename, & fileestats)! = 0) {cout << "cannot get file stats for python script file, /" << filename << "/"! "<< endl; return 0;}

Char * buffer = new char [fileestats.st_size];

INT bytes = FREAD (Buffer, 1, Filestats.st_size, pfile);

Buffer [bytes] = 0; // Terminate the string

Fclose (pfile);

Return buffer;

/ / -------------------------------------------------------------------------------------------- ----------------------------- // name: main () // desc: Application's main entry point point.//--- -------------------------------------------------- ------------------------ Void main (void) {/// // // // // // // // ///// Py_Initialize ();

// Define our custom module called "embedded_test" ... handle <> embedded_test_module (borrowed (PyImport_AddModule ( "embedded_test"))); handle <> embedded_test_init (borrowed (Py_InitModule ( "embedded_test", moduleFuncs)));

// Access Python's "__main__" module ... handle main_module (borrowed (PyImport_AddModule ( "__ main__")));. // Create a dictionary object for the "__main__" module's namespace dict main_namespace (handle <> (borrowed (Pymodule_getdict (main_module.get ())))))))))))))))))))))

Char * pythonscript = readpythonscript ("embedded.py");

Handle <> (pythonscript, py_file_input, main_namespace.ptr (), main_namespace.ptr ()))));

// ONCE The Script Has Finished Executing, Extract One of It Variables. Int nretValue = Extract ("RetValue"]);

Cout << "Script's RetValue Variable Was Set TO:" << nretValue << ENDL;

Delete pythonscript;

////////////////////////////////////////////////////////////////////////////////////>

IF (g_pythoncallback) {int Arg = 123; pyObject * arglist = null; pyobject * result = null;

Arglist = PY_BUILDVALUE ("(i)", arg); result = pyeval_callobject (g_pythoncallback, arglist); py_decref (arglist); if (result! = null) py_decref (result);}

// // cleanup after python ... //

PY_FINALIZE ();

转载请注明原文地址:https://www.9cbs.com/read-34770.html

New Post(0)