Replacing call to PyObject_CallObject with PyEval_CallFunction

Discussion in 'Python' started by grbgooglefan, Jan 30, 2008.

  1. grbgooglefan

    grbgooglefan Guest

    Hello Python Experts
    May you please help me on this change?
    I have following code:
    //==================================================
    typedef struct IOParams {
    char *ioString;
    long lionum;
    double dionum;
    float fionum;
    int nionum;
    } *pIOParams;
    // iterate thru list of variables, check if all req data is set
    int nCtr, ndtyp, ret = 0;
    IOParams inputVar;
    PyObject *pTuple = 0;
    pTuple = PyTuple_New(numofVars);
    for(nCtr = 0; nCtr < numofVars; nCtr++){
    ndtyp = pEvalFunc->pExprVarsArray[nCtr].nDataType;
    switch(ndtyp){
    case(INT_T):
    printf("%s=%d ",pEvalFunc-
    >pExprVarsArray[nCtr].szVarName,inputVar.nionum);


    PyTuple_SetItem(pTuple,nCtr,PyInt_FromLong(inputVar.nionum));
    break;
    case(LONG_T):
    printf("%s=%ld ",pEvalFunc-
    >pExprVarsArray[nCtr].szVarName,inputVar.lionum);


    PyTuple_SetItem(pTuple,nCtr,PyLong_FromLong(inputVar.lionum));
    break;
    case(FLOAT_T):
    printf("%s=%f ",pEvalFunc-
    >pExprVarsArray[nCtr].szVarName,inputVar.fionum);


    PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(inputVar.fionum));
    break;
    case(DOUBLE_T):
    printf("%s=%f ",pEvalFunc-
    >pExprVarsArray[nCtr].szVarName,inputVar.dionum);


    PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(inputVar.dionum));
    break;
    case(STRING_T):
    printf("%s=%s ",pEvalFunc-
    >pExprVarsArray[nCtr].szVarName,inputVar.ioString);


    PyTuple_SetItem(pTuple,nCtr,PyString_FromString(inputVar.ioString));
    break;
    default:
    printf("\nUnknown data type [%d] for %s
    \n",ndtyp,pEvalFunc->pExprVarsArray[nCtr].szVarName);
    bUnknownDataType = true;
    break;
    }
    if(bUnknownDataType){ // got an unknown data type for a variable
    ret = -1;
    break;
    }
    }
    // all variables are set, call Python function
    if(ret == 0){
    printf("\n");
    PyObject *pResult = PyObject_CallObject(pEvalFunc-
    >pPyEvalFunction,pTuple);

    //==================================================
    In this, I am using IOParams struct to set values for the pTuple. The
    number & type of values to be set in pTuple change at runtime and
    therefore I had to put this for loop to set the value in this pTuple.

    Then I came to know about another function call
    PyEval_CallFunction(PyObject *obj, char *format, ...);
    This function call accepts variable number of arguments, like:
    PyEval_CallFunction(obj, "iii", a, b, c);

    Limitation with this is that "a", "b" and "c" are individual variables
    & are all known at compilation time.
    But, in may case, the variables with the values are in a vector of
    IOParams structs.

    How do I pass the elements populated in struct variables of this
    vector dynamically to PyEval_CallFunction, in the fashion somewhat
    like below?
    PyEval_CallFunction(obj, "iii", vector[0].ioparam-
    >nionum,vector[1].ioparam->nionum,vector[2].ioparam->nionum);

    PyEval_CallFunction(obj, "di", vector[0].ioparam-
    >fionum,vector[1].ioparam->nionum);

    PyEval_CallFunction(obj, "diiisis", vector[0].ioparam-
    >fionum,vector[1].ioparam->nionum,vector[2].ioparam-
    >nionum,vector[3].ioparam->nionum,vector[4].ioparam-
    >ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);

    and so on.....

    These function calls are just few samples & not all of them.
    Any suggestions on this.
    Thanks & Regards.
     
    grbgooglefan, Jan 30, 2008
    #1
    1. Advertising

  2. On 30 ene, 01:58, grbgooglefan <> wrote:

    > How do I pass the elements populated in struct variables of this
    > vector dynamically to PyEval_CallFunction, in the fashion somewhat
    > like below?
    > PyEval_CallFunction(obj, "iii", vector[0].ioparam->nionum,vector[1].ioparam->nionum,vector[2].ioparam->nionum);
    >
    > PyEval_CallFunction(obj, "di", vector[0].ioparam->fionum,vector[1].ioparam->nionum);
    >
    > PyEval_CallFunction(obj, "diiisis", vector[0].ioparam->fionum,vector[1].ioparam->nionum,vector[2].ioparam-
    > >nionum,vector[3].ioparam->nionum,vector[4].ioparam-
    > >ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);


    I didn't know of PyEval_CallFunction until now, but aparently the
    lines above are OK. Can't you use it that way? What's your problem?

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 30, 2008
    #2
    1. Advertising

  3. grbgooglefan

    grbgooglefan Guest

    On Jan 30, 1:58 pm, Gabriel Genellina <> wrote:
    > On 30 ene, 01:58, grbgooglefan <> wrote:
    >
    > > How do I pass the elements populated in struct variables of this
    > > vector dynamically to PyEval_CallFunction, in the fashion somewhat
    > > like below?
    > > PyEval_CallFunction(obj, "iii", vector[0].ioparam->nionum,vector[1].ioparam->nionum,vector[2].ioparam->nion­um);

    >
    > > PyEval_CallFunction(obj, "di", vector[0].ioparam->fionum,vector[1].ioparam->nionum);

    >
    > > PyEval_CallFunction(obj, "diiisis", vector[0].ioparam->fionum,vector[1].ioparam->nionum,vector[2].ioparam-
    > > >nionum,vector[3].ioparam->nionum,vector[4].ioparam-
    > > >ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);

    >
    > I didn't know of PyEval_CallFunction until now, but aparently the
    > lines above are OK. Can't you use it that way? What's your problem?
    >
    > --
    > Gabriel Genellina


    slowness of current code - vector is to parsed one by one & switch
    adds another time for checks.
     
    grbgooglefan, Jan 30, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith
    Replies:
    0
    Views:
    498
    John Smith
    Aug 17, 2004
  2. mauro
    Replies:
    1
    Views:
    1,131
    Hrvoje Niksic
    Sep 27, 2007
  3. Sami Vaisanen
    Replies:
    1
    Views:
    387
    Gabriel Genellina
    Oct 20, 2007
  4. grbgooglefan
    Replies:
    5
    Views:
    834
    Fredrik Lundh
    Jan 4, 2008
  5. Carsten Haese
    Replies:
    0
    Views:
    470
    Carsten Haese
    Mar 24, 2008
Loading...

Share This Page