473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Parsing a Python dictionary inside a Python extension

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.
Jul 19 '05 #1
2 2701
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.

Jul 19 '05 #2
qu*****@primenet.com 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
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
1
by: ±è»ó¿Ï | last post by:
I'm a newbie in Python and Python/C API.. I have a simple question. I want to access python dictionary data type from C extension module. ---- spam.c ---- #include <Python.h> PyObject...
4
by: Fuzzyman | last post by:
There have been a couple of config file 'systems' announced recently, that focus on building more powerful and complex configuration files. ConfigObj is a module to enable you to much more *simply*...
13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
1
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.