简体   繁体   English

如何在带有Swig的Python中使用float **?

[英]How to use float ** in Python with Swig?

I am writing swig bindings for some c functions. 我正在为某些C函数编写Swig绑定。 One of these functions takes a float**. 这些功能之一采用浮点数**。 I am already using cpointer.i for the normal pointers and looked into carrays.i , but I did not find a way to declare a float**. 我已经使用cpointer.i作为普通指针并查看了carrays.i ,但是我没有找到声明float **的方法。 What do you recommend? 您有什么推荐的吗?

interface file: 接口文件:

extern int read_data(const char *file,int *n_,int *m_,float **data_,int **classes_); extern int read_data(const char * file,int * n_,int * m_,float ** data_,int ** classes_);

This answer is a repost of one to a related question Framester posted about using ctypes instead of swig. 这个答案是对Framester发布的有关使用ctypes而不是swig的一个相关问题的转发。 I've included it here in case any web-searches turn up a link to his original question. 我将其包括在此处,以防任何网络搜索出现指向他原始问题的链接。

I've used ctypes for several projects now and have been quite happy with the results. 我现在已经将ctypes用于多个项目,并且对结果非常满意。 I don't think I've personally needed a pointer-to-pointer wrapper yet but, in theory, you should be able to do the following: 我认为我个人还不需要指针对指针包装器,但是从理论上讲,您应该能够执行以下操作:

from ctypes import * 

your_dll = cdll.LoadLibrary("your_dll.dll") 

PFloat = POINTER(c_float) 
PInt   = POINTER(c_int) 

p_data    = PFloat() 
p_classes = PInt() 
buff      = create_string_buffer(1024) 
n1        = c_int( 0 ) 
n2        = c_int( 0 ) 

ret = your_dll.read_data( buff, byref(n1), byref(n2), byref(p_data), byref(p_classes) ) 

print('Data:    ', p_data.contents) 
print('Classes: ', p_classes.contents) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM