我正在为一个复杂的C++库编写一个Cython包装器。我想我已经知道了如何编写必要的.pxd和.pyx文件。我现在的问题是,尽管我的C++程序有大约100个不同的名称空间,但Cython编译的python库的名称空间完全是平坦的。
例如,如果我的.pxd文件中有这样的内容:
cdef extern from "lm/io/hdf5/SimulationFile.h" namespace "lm::io::hdf5":
cdef cppclass CppHdf5File "lm::io::hdf5::Hdf5File":
...在我的.pyx文件中:
cdef class Hdf5File:
cdef CppHdf5File* thisptr
...然后,Cython编译的Python库包含一个名为Hdf5File的类。理想情况下,我希望Python包含一个lm.io.hdf5.Hdf5File类(即lm.io.hdf5模块中的一个lm.io.hdf5类)。换句话说,如果有一种方法将C++ ::作用域操作符转换为Python,我希望这样做。点操作符
有办法让Cython很好地处理我现有的C++名称空间吗?
发布于 2022-08-13 11:13:22
假设您的.pyx文件名为source.pyx。我将编写一个setup.py,如下所示:
from setuptools import Extension, setup
from Cython.Build import cythonize
extensions = [
Extension(
name='lm.io.hdf5',
# ^^^^^^^^^^ -- note the name here
sources=[
'path/to/source.pyx',
# other sources like c++ files ...
],
# other options ...
),
]
# Call `setup` as you wish, e.g.:
#setup(
# ext_modules=cythonize(extensions, language_level='3'),
# zip_safe=False,
#)如果编译成功,这将生成lm/io/hdf5.so或类似的内容。然后,在Python中,您可以这样导入:
from lm.io.hdf5 import Hdf5File参考:setuptools文档 ( name字段文档)
https://stackoverflow.com/questions/27894759
复制相似问题