I cant import my function from compiled cython module.

Şansal Birbaş sansal.birbas at alarko-carrier.com.tr
Tue Mar 1 07:05:54 EST 2011


Hi All,
I needed to improve the computation speed of my numpy algorithm which works correctly with
cython. So I created a .pyx file like belows and then compiled to a .pyd file successfully:
***************************************************************************­***************************************
import numpy
cimport cython
cimport numpy as np
DTYPE = np.int
ctypedef np.int_t DTYPE_t
@cython.boundscheck(False)
@cython.wraparound(False)
def CombiHesapla4(np.ndarray[DTYPE_t, ndim=1] X,
                  np.ndarray[DTYPE_t, ndim=1] Y,
                  np.ndarray[DTYPE_t, ndim=1] Z,
                  np.ndarray[DTYPE_t, ndim=1] T,
                  np.ndarray[DTYPE_t, ndim=1] W,
                  np.ndarray[DTYPE_t, ndim=1] istenen,
                  unsigned int max_expander):
    cdef int kalanUO=0
    cdef int m_UI
    cdef int m_UO
    cdef int m_AO
    cdef int m_BO
    cdef int i_AI
    cdef int i_AO
    cdef int i_BI
    cdef int i_BO
    cdef long fiyat
    cdef int possible_max_exp_number
    cdef int possible_max_exp
    combo=[]
    row=[]
    combinations=[]
    cdef long min_price=999999
    cheapest_combi=[]
    cdef np.ndarray toplamIO=numpy.zeros([1, 5], dtype=DTYPE)
    cdef np.ndarray mevcut=numpy.zeros([1, 5], dtype=DTYPE)
    cdef Py_ssize_t i, j, k, l, m
    for i in range(1,5):
        possible_max_exp_number=i*range(max_expander+1)
        possible_max_exp=i*max_expander
        for j in possible_max_exp_number:
            for k in possible_max_exp_number:
                for l in possible_max_exp_number:
                    for m in possible_max_exp_number:
                        if (j+k+l+m)<=(possible_max_exp):
                            mevcut=0
                            mevcut+=X*i
                            mevcut+=Y*j
                            mevcut+=Z*k
                            mevcut+=T*l
                            mevcut+=W*m
                            m_UI=mevcut[0][0]
                            m_UO=mevcut[0][1]
                            m_AO=mevcut[0][2]
                            m_BO=mevcut[0][3]
                            i_AI=istenen[0]
                            i_AO=istenen[1]
                            i_BI=istenen[2]
                            i_BO=istenen[3]
                            if (m_UI>=(i_AI+i_BI)):
                                if ((m_UO+m_BO)>=i_BO):
                                    kalanUO=(m_UO+m_BO)-i_BO
                                    if ((m_BO>=i_BO)):
                                        kalanUO=m_UO
                                    if ((kalanUO+m_AO)>=i_AO):
fiyat=i*X[4]+j*Y[4]+k*Z[4]+l*T[4]+m*W[4]
            combo = [i, j, k, l, m, fiyat]
            combinations.append(combo)
    if len(combinations)>0:
        for row in combinations:
            if (row[5]<min_price):
                min_price=row[5]
                cheapest_combi=row
        toplamIO+=X*cheapest_combi[0]
        toplamIO+=Y*cheapest_combi[1]
        toplamIO+=Z*cheapest_combi[2]
        toplamIO+=T*cheapest_combi[3]
        toplamIO+=W*cheapest_combi[4]
    return cheapest_combi,toplamIO[0]
***************************************************************************­***************************************************
To be able to import this function, I wrote such a program:
(Here _numpytest is the pyd file that I produced from my pyx file and sent to DLL folder of python 2.6.)
***************************************************************************­*******************
import numpy as np
import _numpytest
# Arrays
MELGR200=np.ndarray([0,0,0,0,1239])
MX16160=np.ndarray([16,0,0,16,537])
MEX016U=np.ndarray([16,0,0,0,249])
MEX48U=np.ndarray([8,4,0,0,210])
MEX88U=np.ndarray([8,8,0,0,301])
MEX816U=np.ndarray([16,8,0,0,470])
hedef = np.ndarray([20,0,5,20,0])
print _numpytest.CombiHesapla4(MELGR200,MEX016U,MEX48U,MEX88U,MEX816U,hedef,5)
***************************************************************************­***************
When I try to run this, I get the error "AttributeError: 'module'
object has no attribute 'CombiHesapla4'"
Where do I go wrong?
Thanks a lot.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110301/df7b2532/attachment.html>


More information about the Python-list mailing list