Parsing a Python dictionary inside a Python extension

Discussion in 'Python' started by quadric@primenet.com, May 27, 2005.

  1. Guest

    Hi,

    I would like to pass a dictionary from my Python code to my Python
    extension, extract
    various values from the dictionary (from within the extension) , modify the
    values for the
    relevant keys and return the modified dictionary to Python.

    Can someone point me to an example of what the C code might look like to do
    this?


    I tried something like this and it has not worked.


    static PyObject * ModifyDictionary( PyObject * self , PyObject * args )
    {
    int atab1 = 0 , atab2 = 0;
    PyObject * vdict = NULL;
    PyObject * item = NULL;
    PyObject * ndict = NULL;

    PyArg_ParseTuple( args , "O" , & vdict );

    if ( (item = PyDict_GetItemString( vdict , "atab1")) != NULL
    ) PyArg_ParseTuple( item , "i" , &atab1 );
    if ( (item = PyDict_GetItemString( vdict , "atab2")) != NULL
    ) PyArg_ParseTuple( item , "i" , &atab2 );

    // modify values here and rebuild the dictionary
    // ndict = Py_BuildValue( ........create dictionary here ..........)

    return ndict ;
    }


    Can someone tell me where I am going wrong or point me to an example?

    Thanks for your help.
     
    , May 27, 2005
    #1
    1. Advertising

  2. Guest

    1. Why not to simplfy the problem. You can extract values from the
    dictionari in python (this is fast). Put them in a list (fast). Pass
    the list to the extension, handle it and return to python. etc.

    2. Use Pyrex.
     
    , May 28, 2005
    #2
    1. Advertising

  3. tiissa Guest

    wrote:
    > I tried something like this and it has not worked.


    Oh! What did you ask of it, what did you expect and what did you get?


    > if ( (item = PyDict_GetItemString( vdict , "atab1")) != NULL )
    > PyArg_ParseTuple( item , "i" , &atab1 );


    This code expects a dictionary in which keys 'atab1' and 'atab2' are
    singletons of integer. If that's what you want, I'd say it should work.


    > // ndict = Py_BuildValue( ........create dictionary here ..........)
    >
    > return ndict ;
    > }


    I personnally prefer 'return Py_BuildValue("");' than returning NULL
    pointers (but I don't like to check the doc just to make sure NULL can
    be interpreted as None).

    In order to build a dictionary you could try:

    ....
    return Py_BuildValue("{s:i,s:i}", "key1", atab1, "key2", atab2);
    }

    Or even "{s:(i),s:(i)}" to have singletons for values.


    HTH
     
    tiissa, May 28, 2005
    #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. Ilias Lazaridis
    Replies:
    6
    Views:
    454
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    337
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,164
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    541
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    199
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page