[Pyrex] Convert tuple/list in primitive*

Charles Moad cmoad at indiana.edu
Thu Dec 16 23:07:16 CET 2004


On Dec 16, 2004, at 4:52 PM, Lenard Lindstrom wrote:

> On Thu, 16 Dec 2004 10:21:54 -0500 Charles Moad <cmoad at indiana.edu> 
> wrote:
>
>> Probably another case of being answered before, but I can't find
>> anything.  Given something simple like:
>>
>> cdef extern c_setParam3fv "setParam3fv"(int param, float *value)
>>
>> def setParam3fv(param, value):
>>    cdef float args[3]
>>    cdef int i
>>
>>    i = 0
>>    for val in value:
>>       args[i] = val
>>    c_setParam3fv(param, args)
>>
>> This works fine and all, but I don't think this is my preferred way
>> given a large list of floats since the python looping contruct is 
>> slow.
>>
>> Is there a memcpy type trick I can do, or possibly a cast?
>>
> No. Argument "value" could be any sequence or iterable, and
> the only way to process it is one item at a time. Besides, each
> entry in the sequence is a python float object (a wrapped double)
> which must be converted to a C float. Even the Python C api lacks
> list to C array mapping functions.
>
> Lenard Lindstrom
> <len-l at telus.net>
>

I found the PySequence_Fast method along with related access functions, 
but I have been unable to make it work in pyrex.  Is there a example of 
this out there anywhere that uses the better "for i from 0 <= i < n" 
construct?

Thanks,
	Charlie




More information about the Pyrex mailing list