如何传递python列表地址 [英] How to pass python list address

查看:500
本文介绍了如何传递python列表地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将c ++代码转换为python。
我使用SWIG创建了一个python模块来访问c ++类。

I want to convert c++ code to python. I have created a python module using SWIG to access c++ classes.

现在我想将以下c ++代码传递给Python

Now I want to pass the following c++ code to Python

C ++

#define LEVEL 3
  double thre[LEVEL] = { 1.0l, 1.0e+2, 1.0e+5 };
  GradedDouble gd(LEVEL, thre);
  gd.avg(thre);

我需要将上述代码转换为python

I need to convert the above code to python

用于生成python模块的C ++构造函数

C++ constructor used for generating python module

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界面文件

/ * File:example.i * /

/* File: 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模块函数

A sample python module function

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)

如何转换? p>

How do I convert it?

推荐答案

在SWIG中使用raw double *和length作为接口更加困难。你需要为参数对写一个typemap,可以智能地读取给定大小的数组。更容易使用SWIG的内置向量支持。

Using raw double * and length as an interface is more difficult in SWIG. You'd need to write a typemap for the parameter pair that can intelligently read the array given the size. Easier to use SWIG's built-in vector support.

示例,显示支持:

%module GradedDouble

%{
#include "GradedDouble.h"
%}

%include <std_vector.i>
%include <std_string.i>
%template(DoubleVector) std::vector<double>;
%rename(__repr__) ToString;
%include "GradedDouble.h"



GradedDouble.h



GradedDouble.h

#include <vector>
#include <string>
#include <sstream>

class GradedDouble
{
    std::vector<double> thre_;
public:
    GradedDouble(const std::vector<double>& dbls) : thre_(dbls) {}
    ~GradedDouble() {}
    std::string ToString() const
    {
        std::ostringstream ss;
        ss << "GradedDouble(";
        for(auto d : thre_)
            ss << ' ' << d;
        ss << " )";
        return ss.str();
    }
};



输出



Output

>>> import GradedDouble as gd
>>> x=gd.GradedDouble([1.2,3.4,5.6])
>>> x
GradedDouble( 1.2 3.4 5.6 )

这篇关于如何传递python列表地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆