在cython中包装类模板和函数模板

2024-06-02 04:30:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Cython包装整个C++模板库,以便让它可以用于Python用户。正如我已经提到的,库是类和类的方法的模板,因此,它不是编译的,而是“仅头”。 因为库有点大,所以我决定创建一个文件作为起点,并尝试用cython将其包装起来,让它作为python函数使用。 该文件(我们称之为easy.hpp)反映了库的编写方式

#include <iterator>
#include <utility>
#include <vector>
#include <iostream>

namespace exNamespace {

class exClass
{
public:
   template <typename T1, typename T2> void exMethod(T1 a, T2 b) const;
};

// Impl
template <typename T1, typename T2> void exClass::exMethod(T1 a, T2 b) const
{

    for(auto i=0; i< a.size(); i++){
        std::cout << a[i] << "-" << b[i] << "\n";
    }

    }
}

按照Kurt Smith的“Cython-Python程序员指南”,我构建了两个文件: tWrap.pxdtWrap.pyx

//tWrap.pxd
cdef extern from "easy.hpp" namespace "exNamespace":
    cdef cppclass exClass:
        void exMethod[T1,T2](T1 a, T2, b) except +

//tWrap.pyx
from libcpp.vector cimport vector
cimport tWrap


ctypedef vector[double].iterator vvit

cdef _process(v1, v2):
    cdef vector[double] vv1
    cdef vector[double] vv2
    assert len(v1) == len(v2)        
    for elt1 in v1:
        vv1.push_back(<double>elt1)
    for elt2 in v2:
        vv2.push_back(<double>elt2)
    exClass.exMethod(vv1.begin(),vv2.begin())

def process(v1,v2):
    _process(v1,v2)

setup.py的内容如下:

import os

from setuptools import setup, Extension
from setuptools.dist import Distribution
from Cython.Distutils import build_ext

Distribution(dict(setup_requires='Cython'))

ext_modules = [
     Extension("test", ["tWrap.pyx"],
              language="c++"
            )
]

setup(
    include_package_data=True,
    package_data={'': ['*.pyx', '*.pxd', '*.h', '*.c']},
    cmdclass = {'build_ext': build_ext},
    ext_modules=ext_modules
) 

在编译阶段,我遇到了以下错误:

Error compiling Cython file:
------------------------------------------------------------
...
    assert len(v1) == len(v2)        
    for elt1 in v1:
        vv1.push_back(<double>elt1)
    for elt2 in v2:
        vv2.push_back(<double>elt2)
    exClass.exMethod(vv1.begin(),vv2.begin())
   ^
------------------------------------------------------------

tWrap.pyx:26:4: undeclared name not builtin: exClass

Error compiling Cython file:
------------------------------------------------------------
...
    assert len(v1) == len(v2)        
    for elt1 in v1:
        vv1.push_back(<double>elt1)
    for elt2 in v2:
        vv2.push_back(<double>elt2)
    exClass.exMethod(vv1.begin(),vv2.begin())
                             ^
------------------------------------------------------------

tWrap.pyx:26:30: Cannot convert 'iterator' to Python object

Error compiling Cython file:
------------------------------------------------------------
...
    assert len(v1) == len(v2)        
    for elt1 in v1:
        vv1.push_back(<double>elt1)
    for elt2 in v2:
        vv2.push_back(<double>elt2)
    exClass.exMethod(vv1.begin(),vv2.begin())
                                         ^
------------------------------------------------------------

tWrap.pyx:26:42: Cannot convert 'iterator' to Python object

我不得不承认,我并不完全理解cython如何管理模板,因为在这本书中,作者总是从std库中的模板函数开始

我想实现以下目标:

import test
test.process([1,3,5],[2,4,6])

1-2
3-4
5-6

或者使用具有可打印(<;<;运算符)类型的列表(容器)

更新

正在尝试安装该类:

//tWrap.pyx
...
cdef class cClass:
    cdef tWrap.exClass *_thisptr
    def __cinit__(self):
        self._thisptr = new tWrap.exClass()
    def __dealloc__(self):
        if self._thisptr != NULL:
            del self._thisptr
...

    cdef _process(self,v1, v2):
        cdef vector[double] vv1
        cdef vector[double] vv2
        assert len(v1) == len(v2)        
        for elt1 in v1:
            vv1.push_back(<double>elt1)
        for elt2 in v2:
            vv2.push_back(<double>elt2)
        self._thisptr.exMethod(vv1.begin(),vv2.begin())
...

但编译失败,出现以下错误:

        self._thisptr.exMethod(vv1.begin(),vv2.begin())
                             ^
------------------------------------------------------------

tWrap.pyx:26:30: Call with wrong number of arguments (expected 3, got 2)

Tags: inforlenbackpushv2v1double