2011-07-20 255 views
2

使用C代码我使用下面的代码,我从documentation of scipy错误而蟒蛇

在线

def c_int_binary_search(seq,t): 
    # do a little type checking in Python 
    assert(type(t) == type(1)) 
    assert(type(seq) == type([])) 

    # now the C code 
    code = """ 
     #line 29 "binary_search.py" 
     int val, m, min = 0; 
     int max = seq.length() - 1; 
     PyObject *py_val; 
     for(;;) 
     { 
      if (max < min ) 
      { 
       return_val = Py::new_reference_to(Py::Int(-1)); 
       break; 
      } 
      m = (min + max) /2; 
      val = py_to_int(PyList_GetItem(seq.ptr(),m),"val"); 
      if (val < t) 
       min = m + 1; 
      else if (val > t) 
       max = m - 1; 
      else 
      { 
       return_val = Py::new_reference_to(Py::Int(m)); 
       break; 
      } 
     } 
     """ 
    return inline(code,['seq','t']) 

发现当我试图执行这个脚本的话,我有以下错误

binary_search.py: In function ‘PyObject* compiled_func(PyObject*, PyObject*)’: 
    binary_search.py:36:38: error: ‘Py’ has not been declared 

我想知道是否有人可以指导我这个。我已经安装了PyCXX。我正在使用Ubuntu。

非常感谢。

回答

5

该示例已过时,Py命名空间在最新版本中不存在。

某些发行版通过scipy发布示例(应该保持最新)。在我的机器,还有这个:

/usr/lib64/python2.7/site-packages/scipy/weave/examples/binary_search.py 

如果你没有这样的事情,你可以从SciPy repository下载。