簡體   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