Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Parsing a Python dictionary inside a Python extension

Reply
Thread Tools

Parsing a Python dictionary inside a Python extension

 
 
quadric@primenet.com
Guest
Posts: n/a
 
      05-27-2005
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.


 
Reply With Quote
 
 
 
 
elbertlev@hotmail.com
Guest
Posts: n/a
 
      05-28-2005
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.

 
Reply With Quote
 
 
 
 
tiissa
Guest
Posts: n/a
 
      05-28-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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 "{si),si)}" to have singletons for values.


HTH
 
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
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments