I have a large C struct that I'd like to assign values using a python dictionary. For example:
typedef struct descriptor_t {
char *name
uint32_t user_id
uint32_t group_id
....
(This struct has about 40 members)
Then, there is a function to initialize the descriptor:
init_descriptor(descriptor_t *descriptor_msg)
I wrapped it in Cython and doing the following in the function:
cpdef int allocate(dict job_descriptor)
"""
a = {"name": "foo", "user_id": 1000, "group_id: "1000"}
cdef:
descriptor_t descriptor_msg
init_descriptor(&descriptor_msg)
descriptor_msg.name = a["name"]
descriptor_msg.group_id = a["group_id"]
descriptor_msg.user_id = a["user_id"]
c_allocate(&descriptor_msg)
This works, but I would prefer to do something like the following:
for key in job_descriptor.keys():
descriptor_msg[key] = a[key]
c_allocate(&descriptor_msg)
But I get the error: Attempting to index non-array type 'descriptor_t'
Any suggestions on how to assign python dictionary key values to a C struct of the same key name?
Thanks,
Giovanni