Wrap C struct with array member for access in python: SWIG? cython? ctypes?

后端 未结 5 1125
囚心锁ツ
囚心锁ツ 2021-02-03 10:19

I want to access a C function that returns a struct containing double arrays (where the lengths of these arrays is given by other int members of the struct) from python. The dec

5条回答
  •  春和景丽
    2021-02-03 10:34

    You could always write helper functions that take an "element *" and return the element you seek:

    double element_get_weight(const element *elt, unsigned n) {
        assert(n < elt->ngi);  /* or similar */
        return elt->weight[n];
    }
    

    If you need to modify as well as read, you will want separate "getters" and "setters", of course.

    SWIG should be able to wrap all of these easily and expose them to Python.

    Performance might not be great, but probably no worse than the alternatives.

提交回复
热议问题