如何控制cython cdef类的模块/名称?

mwi*_*ebe 7 c++ python cpython cython

我正在使用cython将C++库暴露给python,方法是将所有包装器对象和函数放在内部模块中_pydynd,然后通过不同的python模块公开它们.
我想控制出现在这些扩展类中的模块和类的名称dynd.nd.array,例如,而不是_pydynd.w_array,它是包装类的内部名称.cython是否有机制来做到这一点?

我希望找到类似于在编写定义时如何重命名C/C++函数的东西,但我的搜索结果却变得干脆.生成的C++代码应该是不同的是tp_name这里的行:

static PyTypeObject __pyx_type_7_pydynd_w_array = {
  PyVarObject_HEAD_INIT(0, 0)
  __Pyx_NAMESTR("_pydynd.w_array"), /*tp_name*/
  sizeof(struct __pyx_obj_7_pydynd_w_array), /*tp_basicsize*/
Run Code Online (Sandbox Code Playgroud)

更新:

如果我尝试直接重命名对象,会发生以下情况:

In [103]: nd.array.__name__
Out[103]: 'w_array'

In [104]: nd.array.__module__
Out[104]: 'dynd._pydynd'

In [105]: nd.array.__name__ = "array"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-105-73d9172a0216> in <module>()
----> 1 nd.array.__name__ = "array"

TypeError: can't set attributes of built-in/extension type 'dynd._pydynd.w_array'

In [106]: nd.array.__module__ = "dynd.nd"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-106-37f990658ede> in <module>()
----> 1 nd.array.__module__ = "dynd.nd"

TypeError: can't set attributes of built-in/extension type 'dynd._pydynd.w_array'
Run Code Online (Sandbox Code Playgroud)

Sve*_*ven 0

我对您的问题有一个部分解决方案,您可以移动或者我们应该说更好地将内容导入到 __ init__.py 文件中的专用命名空间中。

让我们开始,你有一个库,其中包含以下内容:

module.libname.A (A is a class for e.g. or anything else)
module.libname.B
...
Run Code Online (Sandbox Code Playgroud)

现在,我通过复制这些内容来创建自己的库。在 newmodule 中找到的 __ init__.py 中,您可以执行以下操作:

__import__("module."+libname) # this import will import the library in the current context
globals().update(vars(module.libname))

# or use this
library = import_("PathToLibAndFilename", "module.")
globals().update(vars(library))
Run Code Online (Sandbox Code Playgroud)

如果您迭代 vars(library) 中的内容,您还可以重命名内容本身,但到目前为止我还没有尝试过。update() 需要一个字典,你可以传递一个你在布局中选择的字典。

您可以使用以下方法在控制台上进行实验(我认为这对您来说是一个解决方案):

 import(libName)
 globals().update({"testMe":vars(libName)["A"]}) # A is your class name from the library
 # then you can do:
 testMe
 # in my case this outputs something like this: <class 'libName.A'>
Run Code Online (Sandbox Code Playgroud)

PS:我使用 boost::python 来公开我的类,但它只是 cpython 东西的包装器。