如何获得蟒蛇切片与使用痛饮我的C ++数组类工作 [英] How to get python slicing to work with my c++ array class using SWIG

查看:287
本文介绍了如何获得蟒蛇切片与使用痛饮我的C ++数组类工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组类,Array1D,在C ++基本上包装了STL的vector类中定义。我扩展这个类,这样我可以显示阵列向量的单个元素。这里是我的SWIG接口文件中的相关code:

 空间std {
    %模板(dblVector)矢量<双取代;
}%扩展Array1D {
    双__getitem __(INT指数){
        返回(*个体经营)[指数]
    }
}

这使我能够访问数组的单个元素在python:

 >>> A = Array1D(10)#创建一个长度为10的零C ++的矢量
>>>一个[0]
>>> 0

我希望能够调用 A [1:3] 为例,但是,我得到一个类型错误,当我试试这个:

 类型错误:在方法'Array1D___getitem__',参数2类型'诠释'的


解决方案

的问题是,蟒蛇调用的GetItem 和你的函数定义期待一个int切片变异时传递一个切片对象。您将需要编写一个版本的的GetItem 这需要的PyObject *作为参数,然后你不得不实施有矢量的切片。

我没有被设置到实际测试,所以把它当作一粒盐写这个。不过,我会做一些像下面这样。

 %延长Array1D
{
    Array1D * __getitem __(*的PyObject参数)
    {
        如果(PySlice_Check(参数))
        {
            / * Py_ssize_t可能需要在这里,而不是整数* /
            INT LEN = 0,启动= 0,停止= 0,步= 0,slicelength = 0;            LEN =这个 - >大小(); / *或者不过你得到一个向量*的大小/            PySlice_GetIndicesEx((PySliceObject *)参数,LEN,和放大器;启动,和放大器;停止和放大器;一步&安培; slicelength);            / *在这里做的东西,以返回Array1D是正确的片
               考虑到起动/停止上述*定义/步/
        }        / *意外的参数,大概应该在这里抛出一个异常* /
    }
}

I have an an array class, Array1D, defined in c++ which essentially wraps the STL vector class. I extended this class so that I can display individual elements of the array vector. Here is the relevant code in my SWIG interface file:

namespace std{
    %template(dblVector) vector<double>;
}

%extend Array1D{
    double __getitem__(int index) {
        return (*self)[index];
    }
}

This allows me to access individual elements of the array in python:

>>> a = Array1D(10) # creates a c++ vector of length 10 with zeros
>>> a[0]
>>> 0

I want to be able to call a[1:3] for example, however, I get a TypeError when I try this:

TypeError: in method 'Array1D___getitem__', argument 2 of type 'int'

解决方案

The problem is that python passes a Slice object when calling the slice variant of the getitem and your function definition is expecting an int. You will need to write a version of getitem that takes PyObject* as a parameter and then you'd have to implement the slicing of the vector there.

I am writing this without being setup to actually test it, so take it with a grain of salt. But I would do something like the following.

%extend Array1D
{
    Array1D* __getitem__(PyObject *param) 
    {
        if (PySlice_Check(param))
        {
            /* Py_ssize_t might be needed here instead of ints */
            int len = 0, start = 0, stop = 0, step = 0, slicelength = 0;

            len = this->size(); /* Or however you get the size of a vector */

            PySlice_GetIndicesEx((PySliceObject*)param, len, &start, &stop, &step, &slicelength);

            /* Here do stuff in order to return an Array1D that is the proper slice
               given the start/stop/step defined above */
        }

        /* Unexpected parameter, probably should throw an exception here */
    }
}

这篇关于如何获得蟒蛇切片与使用痛饮我的C ++数组类工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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