hello2.c:hello2_caller.pyx:setup.hello2_caller.py:build it:
import and use it:
The compiler complains:
But it works.
UPDATE: the problem is that these two declarations are not the same:
and we need to have the second one.
hello2.c:#include <stdio.h> |
hello2_caller.pyx:cdef extern from "hello2.c": |
setup.hello2_caller.py:from distutils.core import setup |
python setup.hello2_caller.py build_ext --inplace |
>>> import hello2_caller |
In file included from hello2_caller.c:219: |
void f(); |
$ python setup.hello2_caller.py build_ext --inplace |
$ python |
cy.pyx (see below) we have two alternative implementations that compute the Nth element of the Fibonacci series. One version has Cython declarations that the variables are ints (and one for the function itself). The second version is plain Python. In a simple Python script cy.py we do import pyximport |
timeit (at least, that's how I did it). Then we go to the command line and compare:$ python -m timeit -s 'import cypy' 'cypy.f(1000)' |
>>> cy.cyfib(10) |
import numpy |
Cython’s two major use cases [are]: extending the CPython interpreter with fast binary modules, and interfacing Python code with external C libraries.
The source code gets translated into optimized C/C++ code and compiled as Python extension modules
easy_install. Following the tutorial, in hello.pyx I have:def say_hello_to(name): |
setup.hello.py:from distutils.core import setup |
$ python setup.hello.py build_ext --inplace |
hello.so:>>> from hello import say_hello_to |
sqrt function from the C library declared in math.h. I write a file cy_script.pyx:cdef extern from "math.h": |
setup.py with this:from distutils.core import setup |
$ python setup.py build_ext --inplace |
>>> import cy_script |
print f(2); that is, cy_script was actually imported, but the names defined in cy_script are not available to us.cpdef double f(int i):, and it'll work. ] >>> import pyximport |