繁体   English   中英

在 python 中使用 c 代码时出错

[英]Error while using the c code in python

我正在使用我在网上找到的以下代码

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'])

来自scipy 的文档

当我尝试执行此脚本时,出现以下错误

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

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

非常感谢。

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

某些发行版随scipy一起提供示例(应保持最新)。 在我的机器上,有这样的:

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

如果您没有类似的东西,您可以从SciPy 存储库下载它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM