如何在C中创建Python类?

时间:2023-01-13 09:02:46

I have a legacy C library that creates a tree of objects. I would like to convert the tree into a pre-existing Python class. How do I create the PyObject for that class?

我有一个遗留的C库,可以创建一个对象树。我想将树转换为预先存在的Python类。如何为该类创建PyObject?

3 个解决方案

#1


Cython is capable of doing this. It's a semi-fork of Pyrex, and it can wrap existing data structures and expose them to Python. In fact, this is one of the sections in the user guide. Cython is relatively easy to use, and it includes an HTML-output format that shows all the generated code as well as highlighted hot spots where optimization could be applied.

Cython能够做到这一点。它是Pyrex的半分支,它可以包装现有的数据结构并将它们暴露给Python。实际上,这是用户指南中的一个部分。 Cython相对易于使用,它包含一个HTML输出格式,显示所有生成的代码以及可以应用优化的突出显示的热点。

#2


Take a look at generating your Python bindings by using a tool such as pybindgen. These guys are trying to make a superior binding generator, they talk about the shortcomings of other tools (e.g. SWIG) on their front page.

看一下使用pybindgen之类的工具生成Python绑定。这些家伙正试图制作一个优秀的绑定生成器,他们在头版上谈论其他工具(例如SWIG)的缺点。

#3


I've had success using Robin in these scenarios.

在这些场景中我使用Robin取得了成功。

#1


Cython is capable of doing this. It's a semi-fork of Pyrex, and it can wrap existing data structures and expose them to Python. In fact, this is one of the sections in the user guide. Cython is relatively easy to use, and it includes an HTML-output format that shows all the generated code as well as highlighted hot spots where optimization could be applied.

Cython能够做到这一点。它是Pyrex的半分支,它可以包装现有的数据结构并将它们暴露给Python。实际上,这是用户指南中的一个部分。 Cython相对易于使用,它包含一个HTML输出格式,显示所有生成的代码以及可以应用优化的突出显示的热点。

#2


Take a look at generating your Python bindings by using a tool such as pybindgen. These guys are trying to make a superior binding generator, they talk about the shortcomings of other tools (e.g. SWIG) on their front page.

看一下使用pybindgen之类的工具生成Python绑定。这些家伙正试图制作一个优秀的绑定生成器,他们在头版上谈论其他工具(例如SWIG)的缺点。

#3


I've had success using Robin in these scenarios.

在这些场景中我使用Robin取得了成功。