cython - Cython:如何为包含枚举的代码创建 .pxd 文件?
问题描述
我正在尝试“cythonize”以下示例代码,其中包括 Enum 类的实例:
from enum import Enum
class AnimalType(Enum):
Shark = 0
Fish = 1
class Animal:
def __init__(self, animal_type: AnimalType, weight: float):
self.animal_type = animal_type
self.weight = weight
创建带有类型声明的 .pyx 文件很容易:
cpdef enum AnimalType:
Shark = 0
Fish = 1
cdef class Animal:
cdef double weight
cdef AnimalType animal_type
def __init__(self, animal_type: AnimalType, weight: float):
self.animal_type = animal_type
self.weight = weight
但是,我无法将 .pyx 文件拆分为 .pyx 和 .pxd(标头)文件。您能帮我为我的示例定义一个 .pxd 文件吗?
编辑:我已阅读https://groups.google.com/g/cython-users/c/ZoLsLHwnUY4。可能无法做到这一点......
解决方案
它适用于我而无需做任何特别的事情:
睾丸.pxd:
cpdef enum AnimalType:
Shark = 0
Fish = 1
(如果你也想分享 Cython 的定义,你也可以把cdef
部分放在那里)。Animal
testenum.pyx:
# no need to cimport testenum - this happens implicitly
cdef class Animal:
cdef double weight # omit this if you put it in the pxd file
cdef AnimalType animal_type # omit this if you put it in the pxd file
def __init__(self, animal_type: AnimalType, weight: float):
self.animal_type = animal_type
self.weight = weight
其他文件.pyx:
from testenum cimport AnimalType
cdef class C:
cdef AnimalType at
推荐阅读
- json - 如何使用邮递员在错误消息中返回键/值进行 ajv 验证
- angularjs - 对于复选框,ng-dropdown-multiselect 在 angularjs 中与 ng-disabled 一起使用
- c - 如何使系统函数调用起作用?
- reactjs - 可重用的反应组件将数据动态绑定到多个列表中的多个下拉列表
- java - 使用罗马数字删除编号
- mysql - 每次使用mysql时`join`都会生成笛卡尔积吗?
- javascript - 有没有办法使用 Nightmare 或类似的方法来访问 json 响应?
- url-rewriting - 如何制定此重写规则以便它可以接受查询字符串?
- java - 在 spring-data-elasticsearch 项目的 Criteria 类中使用 not() 方法时出现异常
- mysql - 对于有两位作者的每本书的选择标题、出版商名称、类型和作者姓名,没有得到任何结果