C programs are often written in an "object oriented style" where functions take a pointer to a typedef'ed struct as the first argument. Using this huristic and modifying PyCParser's AST in place, we can reconstruct a Cython class that has proper methods. This provides an automated way to convert a C API into clean object oriented API.
Example C API
// basic typedef struct
typedef struct A {
int x;
float y;
} A;
void set_x( A *a, int x ) {
a->x = x;
}
int get_x( A *a ) {
return a->x;
}
Automatic Object Orientation
cdef class A:
cdef public int x
cdef public float y
def __init__(self, int x,float y):
self.x = x
self.y = y
cpdef void set_x(self, int x):
self.x = x
cpdef int get_x(self):
return self.x
source code
Dear Harts,
ReplyDeleteCould you please explain the usage of your code ?
Suppose I have a *.c and a *.h files, how can I generate cython code ?