开发者

Error while using the c code in python

开发者 https://www.devze.com 2023-03-21 22:59 出处:网络
I am using the following code which I have found online def c_int_binary_search(seq,t): # do a little type checking in Python

I am using the following code which I have found online

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 = """
     开发者_如何学JAVA  #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'])

from the documentation of scipy

When I try to execute this script then i have the following errors

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

I am wondering if someone can guide me in this. I have already installed PyCXX. I am using Ubuntu.

Thanks a lot.


That example is out of date, the Py namespace doesn't exist in recent versions.

Some distributions ship the examples (that should be kept up to date) with scipy. On my machine, there's this:

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

If you don't have something like that, you can download it from SciPy repository.

0

精彩评论

暂无评论...
验证码 换一张
取 消