Ctypes, available from Python's standart library, provides C compatible data types, and allows calling functions or shared libraries. It can be used to wrap these libraries in pure Python... but requires (some) additionnal work
Numpy's array have a ctypes attribute which contains the address of the underlying buffer. It is the user's responsiblity to ensure data are contiguous and properly alligned !
In [1]:
import numpy
a = numpy.arange(10)
print(a)
print(a.strides)
print("Address of the buffer: %s" % a.ctypes.data)
print(a.flags)
b=a[::2]
print(b)
print(b.strides)
print("Address of the buffer: %s" % b.ctypes.data)
print(b.flags)
Fortran to Python interface generator, provided by NumPy, which allows:
Of course F2Py needs a ForTran Compiler to compile Fortran code which can be an issue on some platforms.
In [2]:
src = """
C FILE: FIB3.F
SUBROUTINE FIB(A,N)
C
C CALCULATE FIRST N FIBONACCI NUMBERS
C
INTEGER N
INTEGER A(N)
Cf2py intent(in) n
Cf2py intent(out) a
Cf2py depend(n) a
DO I=1,N
IF (I.EQ.1) THEN
A(I) = 0
ELSEIF (I.EQ.2) THEN
A(I) = 1
ELSE
A(I) = A(I-1) + A(I-2)
ENDIF
ENDDO
END
C END FILE FIB3.F"""
Note: Fortran has no "functions" only "subroutines" (or procedure), so all output needs to be given by calling module.
The 3 comment lines staring with Cf2py declare which variable are input, and which are output
In [3]:
from numpy import f2py
In [4]:
f2py.compile(src, "fibo")
Out[4]:
In [5]:
import fibo
#reload(fibo)
In [6]:
fibo.fib(19)
Out[6]:
Weave is part of SciPy, it is a runtime compiler of C/C++ code to make loop go fast but it is deprecated now and remains only for compatibility reasons. While working effectively under UNIX, weave is had to set-up on windows computer (due to the absence of any kind of compiler by default).
As Python is written in C, any line of Python can be translated to its equivalent C part, using metaprogramming. The Pyrex project aimed at infering types to make the generated C-code easier to optimize for the compiler. Cython is the continuation of the Pyrex project with support for NumPy nd-arrays.
Cython is the weave killer: many project replaced their hand-written binding or C code by Cython code. Used by LXML and most of the scikits
C++ binding for python from the famous C++ boost library. Very large but also very efficient. It is used by many projects: PyOpenCL, PyCuda.
General purpose binding for any kind of interpreted programming language
The Python-C++ binding from PyQt.
The Python-C++ binding from PySide.
The Python-C interface invented by PyPy
Well number crunching is exciting for geeks but most of the data-analysts spent their time at looking at data and thinking how to represent them. Is there a "good" visualization toolkit in Python ? Matplotlib
In [ ]: