Pyrex is a useful tool for writing Python extensions. Because the
Pyrex language is similar to Python, writing extensions
in**Pyrex** is easier than doing so in C.
More information on Pyrex is
athttp://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/.
Here is a simple function definition in Pyrex.
# python_201_pyrex_string.pyx
import string
def formatString(object s1, object s2):
s1 = string.strip(s1)
s2 = string.strip(s2)
s3 = '<<%s||%s>>' % (s1, s2)
s4 = s3 * 4
return s4
Download as text (original file name:
Examples/python_201_pyrex_string.pyx).
And, here is a make file:
CFLAGS = -DNDEBUG -O3 -Wall -Wstrict-prototypes -fPIC \
-I/usr/local/include/python2.3
all: python_201_pyrex_string.so
python_201_pyrex_string.so: python_201_pyrex_string.o
gcc -shared python_201_pyrex_string.o -o python_201_pyrex_string.so
python_201_pyrex_string.o: python_201_pyrex_string.c
gcc -c ${CFLAGS} python_201_pyrex_string.c -o python_201_pyrex_string.o
python_201_pyrex_string.c: python_201_pyrex_string.pyx
pyrexc python_201_pyrex_string.pyx
clean:
rm -f python_201_pyrex_string.so python_201_pyrex_string.o \
python_201_pyrex_string.c
Download as text (original file name:
Examples/python_201_pyrex_makestring).
Here is another example. In this one, one function in the.pyx file
calls another. Here is the implementation file:
# python_201_pyrex_primes.pyx def showPrimes(int kmax): plist =
primes(kmax) for p in plist: print ‘prime: %d’ % p cdef primes(int
kmax): cdef int n, k, i cdef int p[1000] result = [] if kmax > 1000:
kmax = 1000 k = 0 n = 2 while k < kmax: i = 0 while i < k and n % p[i]
0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
return result
Download as text (original file name:
Examples/python_201_pyrex_primes.pyx).
And, here is a make file:
#CFLAGS = -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -fPIC \
# -I/usr/local/include/python2.3
CFLAGS = -DNDEBUG -I/usr/local/include/python2.3
all: python_201_pyrex_primes.so
python_201_pyrex_primes.so: python_201_pyrex_primes.o
gcc -shared python_201_pyrex_primes.o -o python_201_pyrex_primes.so
python_201_pyrex_primes.o: python_201_pyrex_primes.c
gcc -c ${CFLAGS} python_201_pyrex_primes.c -o python_201_pyrex_primes.o
python_201_pyrex_primes.c: python_201_pyrex_primes.pyx
pyrexc python_201_pyrex_primes.pyx
clean:
rm -f python_201_pyrex_primes.so python_201_pyrex_primes.o \
python_201_pyrex_primes.c
Download as text (original file name:
Examples/python_201_pyrex_makeprimes).
Here is the output from running the makefile:
$ make -f python_201_pyrex_makeprimes clean
rm -f python_201_pyrex_primes.so python_201_pyrex_primes.o \
python_201_pyrex_primes.c
$ make -f python_201_pyrex_makeprimes
pyrexc python_201_pyrex_primes.pyx
gcc -c -DNDEBUG -I/usr/local/include/python2.3 python_201_pyrex_primes.c -o python_201_pyrex_primes.o
gcc -shared python_201_pyrex_primes.o -o python_201_pyrex_primes.so
Here is an interactive example of its use:
$ python
Python 2.3b1 (#1, Apr 25 2003, 20:36:09)
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import python_201_pyrex_primes
>>> dir(python_201_pyrex_primes)
['__builtins__', '__doc__', '__file__', '__name__', 'showPrimes']
>>> python_201_pyrex_primes.showPrimes(5)
prime: 2
prime: 3
prime: 5
prime: 7
prime: 11
This next example shows how to use Pyrex to implement a new extension
type, that is a new Python built-in type. Notice that the class is
declared with the cdef keyword, which tells Pyrex to generate the C
implementation of a type instead of a class.
Here is the implementation file:
# python_201_pyrex_clsprimes.pyx “”“An implementation of primes
handling class for a demonstration of Pyrex. “”” cdef class Primes:
“”“A class containing functions for handling primes. “”” def
showPrimes(self, int kmax): “”“Show a range of primes. Use the method
primes() to generate the primes. “”” plist = self.primes(kmax) for p
in plist: print ‘prime: %d’ % p def primes(self, int kmax):
“”“Generate the primes in the range 0 - kmax. “”” cdef int n, k, i
cdef int p[1000] result = [] if kmax > 1000: kmax = 1000 k = 0 n = 2
while k < kmax: i = 0 while i < k and n % p[i]
0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
return result
Download as text (original file name:
Examples/python_201_pyrex_clsprimes.pyx).
And, here is a make file:
CFLAGS = -DNDEBUG -I/usr/local/include/python2.3
all: python_201_pyrex_clsprimes.so
python_201_pyrex_clsprimes.so: python_201_pyrex_clsprimes.o
gcc -shared python_201_pyrex_clsprimes.o -o python_201_pyrex_clsprimes.so
python_201_pyrex_clsprimes.o: python_201_pyrex_clsprimes.c
gcc -c ${CFLAGS} python_201_pyrex_clsprimes.c -o python_201_pyrex_clsprimes.o
python_201_pyrex_clsprimes.c: python_201_pyrex_clsprimes.pyx
pyrexc python_201_pyrex_clsprimes.pyx
clean:
rm -f python_201_pyrex_clsprimes.so python_201_pyrex_clsprimes.o \
python_201_pyrex_clsprimes.c
Download as text (original file name:
Examples/python_201_pyrex_makeclsprimes).
Here is output from running the makefile:
$ make -f python_201_pyrex_makeclsprimes clean
rm -f python_201_pyrex_clsprimes.so python_201_pyrex_clsprimes.o \
python_201_pyrex_clsprimes.c
$ make -f python_201_pyrex_makeclsprimes
pyrexc python_201_pyrex_clsprimes.pyx
gcc -c -DNDEBUG -I/usr/local/include/python2.3 python_201_pyrex_clsprimes.c -o python_201_pyrex_clsprimes.o
gcc -shared python_201_pyrex_clsprimes.o -o python_201_pyrex_clsprimes.so
And here is an interactive example of its use:
$ python
Python 2.3b1 (#1, Apr 25 2003, 20:36:09)
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import python_201_pyrex_clsprimes
>>> dir(python_201_pyrex_clsprimes)
['Primes', '__builtins__', '__doc__', '__file__', '__name__']
>>> primes = python_201_pyrex_clsprimes.Primes()
>>> dir(primes)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__', 'primes', 'showPrimes']
>>> primes.showPrimes(4)
prime: 2
prime: 3
prime: 5
prime: 7
Documentation – Also notice that Pyrex preserves the
documentation for the module, the class, and the methods in the class.
You can show this documentation with pydoc, as follows:
$ pydoc python_201_pyrex_clsprimes
Or, in Python interactive mode, use:
$ python
Python 2.3b1 (#1, Apr 25 2003, 20:36:09)
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import python_201_pyrex_clsprimes
>>> help(python_201_pyrex_clsprimes)