wl2776 - 5 months ago 34

C Question

I'm developing a python 2.7 module, that runs compiled function from a dynamic library using

`ctypes`

Dynamic library performs deep copy of data, specially for python wrapper.

Further processing in the module is done using

`numpy`

`numpy.ndarray`

Speed and memory consumption is not an issue for now.

For now, I've implemented a method in that class, that creates and returns

`numpy.ndarray`

`numpy.frombuffer`

I'm wondering, if it can be implemented better.

Here is the python class

`import ctypes as ct`

import numpy as np

class C_Mat(ct.Structure):

_fields_ = [("rows", ct.c_int),

("cols", ct.c_int),

("data", ct.c_char_p),

("step", ct.ARRAY(ct.c_int64, 2)),

("data_type", ct.c_int)]

_dtypes = { 0: np.uint8,

1: np.int8,

2: np.uint16,

3: np.int16,

4: np.int32,

5: np.float32,

6: np.float64 }

def image(self):

r = np.frombuffer(self.data,

dtype=self._dtypes[self.data_type],

count=self.cols*self.step[1]*self.step[0])

r.shape = (self.cols, self.rows)

r.strides = (self.step[0], self.step[1])

return r

Answer

There is the Array Interface description https://docs.scipy.org/doc/numpy/reference/arrays.interface.html