我想将 c++ 代码转换为 python。我使用 SWIG 创建了一个 python 模块来访问 c++ 类。
现在我想将以下 c++ 代码传递给 Python
C++
#define LEVEL 3
double thre[LEVEL] = { 1.0l, 1.0e+2, 1.0e+5 };
GradedDouble gd(LEVEL, thre);
gd.avg(thre);
我需要将上面的代码转换为python
用于生成 python 模块的 C++ 构造函数
GradedDouble::GradedDouble(int n, double *thre)
{
n_ = n;
for (int i = 0; i < n_; ++i)
{
thre_.push_back(thre[i]);
grade_.push_back(new grade_type());
}
}
SWIG 接口文件
/* 文件:example.i */
%module example
%include typemaps.i
%apply double * INPUT { double *}
%apply double * INPUT { double *buf };
%{
#include "Item.h"
#include "GradedComplex.h"
#include "GradedDouble.h"
void avg(double *buf);
%}
%include <std_string.i>
%include <std_complex.i>
%include "Item.h"
%include "GradedComplex.h"
%include "GradedDouble.h"
%template(Int) Item<int>;
%template(Double) Item<double>;
%template(Complex) Item<std::complex<double> >;
一个示例 python 模块函数
class GradedDouble(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, GradedDouble, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, GradedDouble, name)
__repr__ = _swig_repr
def __init__(self, *args):
this = _example.new_GradedDouble(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _example.delete_GradedDouble
__del__ = lambda self : None;
def push(self, *args): return _example.GradedDouble_push(self, *args)
def avg(self, *args): return _example.GradedDouble_avg(self, *args)
GradedDouble_swigregister = _example.GradedDouble_swigregister
GradedDouble_swigregister(GradedDouble)
我该如何转换它?