2017-07-26 52 views
0

http://earthpy.org/speed.html我發現下面的如何在這個環節上使用用Cython編譯器在Python

%%cython 
import numpy as np 

def useless_cython(year): 

    # define types of variables 
    cdef int i, j, n 
    cdef double a_cum 

    from netCDF4 import Dataset 
    f = Dataset('air.sig995.'+year+'.nc') 
    a = f.variables['air'][:] 

    a_cum = 0. 
    for i in range(a.shape[0]): 
     for j in range(a.shape[1]): 
      for n in range(a.shape[2]): 
       #here we have to convert numpy value to simple float 
       a_cum = a_cum+float(a[i,j,n]) 

    # since a_cum is not numpy variable anymore, 
    # we introduce new variable d in order to save 
    # data to the file easily 
    d = np.array(a_cum) 
    d.tofile(year+'.bin') 
    print(year) 
    return d 

這似乎是那麼容易,因爲只寫了功能%%用Cython。然而,這只是對我無效 - 「」聲明似乎沒有效果「,我的IDE說。 經過一些研究後,我發現%%語法來自我也安裝的iphyton(以及cython)。仍然沒有工作。 Iam使用python3.6 任何想法?

回答

0

一旦你在IPython解釋器中,你必須在使用它之前加載擴展名。它可以用語句%load_ext完成,所以你的情況:

%load_ext cython

這兩個工具都非常有據可查的,如果你還沒有看到它,看看對文檔的相關部分cython sideIPython side

相關問題