开发者

Accessing the content of a variable array with ctypes

I use ctypes to access a file reading C functio开发者_如何学JAVAn in python. As the read data is huge and unknown in size I use **float in C .

int read_file(const char *file,int *n_,int *m_,float **data_) {...}

The functions mallocs an 2d array, called data, of the appropriate size, here n and m, and copies the values to the referenced ones. See following snippet:

*data_ = data;
*n_ = n;
*m_ = m;

I access this function with the following python code:

p_data=POINTER(c_float)
n=c_int(0)
m=c_int(0)
filename='datasets/usps'
read_file(filename,byref(n),byref(m),byref(p_data))

Afterwards I try to acces p_data using contents, but I get only a single float value.

p_data.contents
c_float(-1.0)

My question is: How can I access data in python?

What do you recommended? Please don't hesitate to point out if I left something unclear!


might be simpler to do the whole thing in python with the struct library. but if you're sold on ctypes (and I don't blame you, it's pretty cool):

#include <malloc.h>
void floatarr(int* n, float** f)
{
    int i;
    float* f2 = malloc(sizeof(float)*10);
    n[0] = 10;
    for (i=0;i<10;i++)
    { f2[i] = (i+1)/2.0; }
    f[0] = f2;
}

and then in python:

from ctypes import *

fd = cdll.LoadLibrary('float.dll')
fd.floatarr.argtypes = [POINTER(c_int),POINTER(POINTER(c_float))]

fpp = POINTER(c_float)()
ip = c_int(0)
fd.floatarr(pointer(ip),pointer(fpp))
print ip
print fpp[0]
print fpp[1]

the trick is that capitals POINTER makes a type and lowercase pointer makes a pointer to existing storage. you can use byref instead of pointer, they claim it's faster. I like pointer better because it's clearer what's happening.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜