0

I am a newbie in Python and C and I would like to know how to put dictionary elements into a C like structure (struct).

For example, here is my structure:

typedef struct
{
    int dim;
    float *Tab1;
    float *Tab2;
}  
Tableaux;

Here is my dictionary in Python:

Tableaux = {}
Tableaux["dim"]=None
Tableaux["Tab1"]=[]
Tableaux["Tab2"]=[]

Here is my interface function:

static PyObject* py_initTab(PyObject* self, PyObject* args)
{
    PyObject* dict;
    Tableaux Tab;
    if (!PyArg_ParseTuple(args, "O!", &dict))  
        return NULL;        

     Tab.Tab1=dict["Tab1"]; // How could I do something like that?      

     return Py_BuildValue("");
}
SOCKet
  • 191
  • 1
  • 2
  • 15

1 Answers1

2

You can use PyDict_GetItem() for that:

PyObject* pytab1 = PyDict_GetItemString(dict, "Tab1");

Since the result is a list, you can use these calls to examine it

This documentation explains how to convert primitive types between C and Python.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820