Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [ Please help ] how to create Python functions in C++ at runtime &call them

Reply
Thread Tools

[ Please help ] how to create Python functions in C++ at runtime &call them

 
 
grbgooglefan
Guest
Posts: n/a
 
      11-21-2007
I want to compile following type of python function in my C++ program
at runtime.
def isSizeSmall(size,vol,ADV,prod):
if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod=="Stock")):
print "OK"; return 10
else: print "NOK"; return 11

Using Py_CompileString, I compiled a code object from this function,
like:
char szExpr[2048];
sprintf(szExpr,"def isSizeSmall(size,vol,ADV,prod):\n if ( (size <
1000) & (vol < (0.001 * ADV)) & (prod==\"Stock\")): print \"OK\";
return 10\n else: print \"NOK\"; return 11\n\n\n");

PyObject* result = Py_CompileString(szExpr,"<string>",
Py_file_input);

Then, I tried to call this function in 3 different ways: 1)
PyEval_EvalCode, 2) PyObject_CallObject.
But both failed.

For using PyObject_CallObject, I did something like this.

PyObject* tuple = PyTuple_New(4);
PyObject* val = 0;

val = PyInt_FromLong(ordval.size);
PyTuple_SetItem(tuple,0,val);

val = PyInt_FromLong(ordval.vol);
PyTuple_SetItem(tuple,1,val);

val = PyInt_FromLong(ordval.ADV);
PyTuple_SetItem(tuple,2,val);

val = PyString_FromString(ordval.prod);
PyTuple_SetItem(tuple,3,val);

PyObject *glb = PyDict_New();
PyDict_SetItemString(glb, "__builtins__", PyEval_GetBuiltins());

PyObject* func = PyFunction_New(result,glb);
if(!func || PyErr_Occurred()){
printf("Failed to get Function..\n");
PyErr_Print();
} else {
printf("Calling PyObject_CallObject\n");
if(PyCallable_Check(func))
printf("func is callable\n");
PyObject* ret = PyObject_CallObject(func, tuple);
if(!ret || PyErr_Occurred())
PyErr_Print();
else
printf("PyObject_CallObject evaluated..\n");
}

I got this output. Why is it so? Why is this function not taking any
parameters? Please help.

Expression to eval =
[def isSizeSmall(size,vol,ADV,prod):
if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod=="Stock")): print
"OK"; return 10
else: print "NOK"; return 11

]
func is callable
TypeError: ?() takes no arguments (4 given)

Thanks for all help & guidance
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Compacting 5,000 Messages, Moving Them To A Google Account, And Burning Them Onto A CD Martin Computer Support 9 01-14-2007 09:33 PM
HttpHandlers - Learn Them. Use Them. Anonieko ASP .Net 5 06-16-2006 06:19 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Does anyone know them or has any experience with them (good or bad)? Mike Timbell Digital Photography 9 11-13-2003 01:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57