Separate extension types in Cython for static typing

I converted the Python class to an extension type inside the .pyx file. I can create this object in another Cython module, but I cannot do static printing with it.

Here is part of my class:

cdef class PatternTree: cdef public PatternTree previous cdef public PatternTree next cdef public PatternTree parent cdef public list children cdef public int type cdef public unicode name cdef public dict attributes cdef public list categories cdef public unicode output def __init__(self, name, parent=None, nodeType=XML): # Some code cpdef int isExpressions(self): # Some code cpdef MatchResult isMatch(self, PatternTree other): # Some code # More functions... 

I tried to use the .pxd file to declare it, but it says that the "C method [some function] is declared but not defined" for all my functions. I also tried to strip the C stuff in the functions of my implementation to make it act like an extended class, but that didn't work either.

Here is my .pxd:

 cdef class PatternTree: cdef public PatternTree previous cdef public PatternTree next cdef public PatternTree parent cdef public list children cdef public int type cdef public unicode name cdef public dict attributes cdef public list categories cdef public unicode output # Functions cpdef int isExpressions(self) cpdef MatchResult isMatch(self, PatternTree other) 

Thank you for your help!

+7
python cython
source share
1 answer

I learned about it. Here is the solution:

In .pyx:

 cdef class PatternTree: # NO ATTRIBUTE DECLARATIONS! def __init__(self, name, parent=None, nodeType=XML): # Some code cpdef int isExpressions(self): # Some code cpdef MatchResult isMatch(self, PatternTree other): # More code 

In .pxd:

 cdef class PatternTree: cdef public PatternTree previous cdef public PatternTree next cdef public PatternTree parent cdef public list children cdef public int type cdef public unicode name cdef public dict attributes cdef public list categories cdef public unicode output # Functions cpdef int isExpressions(self) cpdef MatchResult isMatch(self, PatternTree other) 

In any Cython module (.pyx) I want to use this for:

 cimport pattern_tree from pattern_tree cimport PatternTree 

One final warning: Cython does not support relative imports . This means that you need to provide the entire module path relative to the main file from which you are executing.

Hope this helps someone out there.

+8
source share

All Articles