我想为我的C库创建一个python包装器.如果将std :: vector自动转换为python列表,并且反过来进行转换,那将很酷.
不幸的是,如果我将此代码添加到我的接口文件中,我仍然会在运行时出错.
%typemap(in) std::vector value (std::vector vIn) {
int iLen = PySequence_Length($input);
for(unsigned int i = 0; i < iLen; i++) {
PyObject *o = PySequence_GetItem($input, i);
if (PyNumber_Check(o)) {
vIn.push_back((float)PyFloat_AsDouble(o) );
}
}
$1 = vIn;
}
%typemap(out) std::vector {
std::vector vOut = $1;
int iLen = vOut.size();
$result = PyList_New(iLen);
for(unsigned int i = 0; i < iLen; i++) {
double fVal = vOut.at(i);
PyObject *o = PyFloat_FromDouble((double) fVal);
PyList_SetItem($result, i, o);
}
}
类头:
class TrainingSet {
private:
std::vector<:vector> > m_vInputList;
std::vector<:vector> > m_vOutputList;
public:
void AddInput(const std::vector &vIn);
// ..
Python代码:
trainSet = TrainingSet()
trainSet.AddInput([0.5, 0.5, 0.5])
错误:
File "runSOMNet.py", line 9, in
trainSet.AddInput([0.5, 0.5, 0.5])
File "/home/dgrat/annetgpgpu/build/ANNet/ANPyNetCPU.py", line 674, in AddInput
def AddInput(self, *args): return _ANPyNetCPU.TrainingSet_AddInput(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'TrainingSet_AddInput'.
Possible C/C++ prototypes are:
ANN::TrainingSet::AddInput(std::vector< float,std::allocator< float > > const &)
ANN::TrainingSet::AddInput(float *,unsigned int const &)