2013-07-18 17 views
7

Bir Python sınıfını .pyx dosyasının içindeki bir uzantı türüne dönüştürdüm. Bu nesneyi diğer Cython modülünde oluşturabilirim, ancak , statik yazmayı onunla yapamaz. İşte Statik yazım için Cython'da paylaşım uzantısı türleri

benim sınıfının bir parçasıdır: Bunu bildirmek için bir .pxd dosyasını kullanarak denedi

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... 

, ancak "C yöntemi [bazı fonksiyon] beyan edildi, ancak tanımlı değil" tüm diyor benim fonksiyonum Ayrıca, C öğelerini, uygulamamın işlevlerinde, artırılmış bir sınıf gibi davranmasını sağlamak için kullanmayı denedim, ancak bu da işe yaramadı. bu haliyle

İşte benim .pxd geçerli: Yardımlarınız için

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) 

teşekkürler!

cevap

8

Bunun için bir çözüm buldum. .pyx olarak

: .pxd olarak

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 

:

cimport pattern_tree 
from pattern_tree cimport PatternTree 
: Ben bu kullanmak istiyorum

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) 

neyse Cython modülü (.pyx) olarak İşte çözümü

Son uyarı uyarısı: Cython , numaralı göreceli içeri aktarmayı desteklemez. Bu, tüm modül yolunu yürütmekte olduğunuz ana dosyaya göre sağlamanız gerektiği anlamına gelir.

Bu konuda yardımcı olur.

+0

Daha karmaşık bir proje dizininiz varsa, her bir klasörde __init__.py olduğundan emin olun. Aksi takdirde, bazı özelliklerin mevcut olmadığı konusunda bir hata alırsınız. – emschorsch

İlgili konular