Commit bd2a3d43 authored by ggellner@encolpuis's avatar ggellner@encolpuis

Made cython highlighting the default in all files.

parent e5d24fd8
.. highlight:: cython
.. _early-binding-speed-label: .. _early-binding-speed-label:
Early Binding for Speed Early Binding for Speed
...@@ -17,7 +19,9 @@ slowness compared to 'early binding' languages such as C++. ...@@ -17,7 +19,9 @@ slowness compared to 'early binding' languages such as C++.
However with Cython it is possible to gain significant speed-ups through the However with Cython it is possible to gain significant speed-ups through the
use of 'early binding' programming techniques. use of 'early binding' programming techniques.
For example, consider the following (silly) code example:: For example, consider the following (silly) code example:
.. sourcecode:: cython
cdef class Rectangle: cdef class Rectangle:
cdef int x0, y0 cdef int x0, y0
...@@ -38,7 +42,9 @@ In the :func:`rectArea` method, the call to :meth:`rect.area` and the ...@@ -38,7 +42,9 @@ In the :func:`rectArea` method, the call to :meth:`rect.area` and the
:meth:`.area` method contain a lot of Python overhead. :meth:`.area` method contain a lot of Python overhead.
However, in Cython, it is possible to eliminate a lot of this overhead in cases However, in Cython, it is possible to eliminate a lot of this overhead in cases
where calls occur within Cython code. For example:: where calls occur within Cython code. For example:
.. sourcecode:: cython
cdef class Rectangle: cdef class Rectangle:
cdef int x0, y0 cdef int x0, y0
...@@ -71,7 +77,9 @@ Rectangle. By using this declaration, instead of just dynamically assigning to ...@@ -71,7 +77,9 @@ Rectangle. By using this declaration, instead of just dynamically assigning to
But Cython offers us more simplicity again, by allowing us to declare But Cython offers us more simplicity again, by allowing us to declare
dual-access methods - methods that can be efficiently called at C level, but dual-access methods - methods that can be efficiently called at C level, but
can also be accessed from pure Python code at the cost of the Python access can also be accessed from pure Python code at the cost of the Python access
overheads. Consider this code:: overheads. Consider this code:
.. sourcecode:: cython
cdef class Rectangle: cdef class Rectangle:
cdef int x0, y0 cdef int x0, y0
...@@ -90,7 +98,7 @@ overheads. Consider this code:: ...@@ -90,7 +98,7 @@ overheads. Consider this code::
rect = Rectangle(x0, y0, x1, y1) rect = Rectangle(x0, y0, x1, y1)
return rect.area() return rect.area()
.. note:: .. Note::
in earlier versions of Cython, the :keyword:`cpdef` keyword is in earlier versions of Cython, the :keyword:`cpdef` keyword is
:keyword:`rdef` - but has the same effect). :keyword:`rdef` - but has the same effect).
......
.. highlight:: cython
Extension Types Extension Types
=============== ===============
Introduction Introduction
------------ -------------
As well as creating normal user-defined classes with the Python class As well as creating normal user-defined classes with the Python class
statement, Cython also lets you create new built-in Python types, known as statement, Cython also lets you create new built-in Python types, known as
...@@ -34,7 +35,7 @@ extension types to wrap arbitrary C data structures and provide a Python-like ...@@ -34,7 +35,7 @@ extension types to wrap arbitrary C data structures and provide a Python-like
interface to them. interface to them.
Attributes Attributes
---------- -----------
Attributes of an extension type are stored directly in the object's C struct. Attributes of an extension type are stored directly in the object's C struct.
The set of attributes is fixed at compile time; you can't add attributes to an The set of attributes is fixed at compile time; you can't add attributes to an
......
.. highlight:: cython
Interfacing with External C Code Interfacing with External C Code
================================ ================================
......
.. highlight:: cython
.. _language-basics-label: .. _language-basics-label:
Language Basics Language Basics
......
.. highlight:: cython
.. _cython-limitations-label: .. _cython-limitations-label:
************* *************
......
.. highlight:: cython
.. _numpy_tute-label: .. _numpy_tute-label:
************************** **************************
...@@ -220,9 +222,7 @@ Adding types ...@@ -220,9 +222,7 @@ Adding types
============= =============
To add types we use custom Cython syntax, so we are now breaking Python source To add types we use custom Cython syntax, so we are now breaking Python source
compatibility. Here's :file:`convolve2.pyx`. *Read the comments!* compatibility. Here's :file:`convolve2.pyx`. *Read the comments!* ::
.. code-block:: cython
from __future__ import division from __future__ import division
import numpy as np import numpy as np
...@@ -339,9 +339,7 @@ not provided then one-dimensional is assumed). ...@@ -339,9 +339,7 @@ not provided then one-dimensional is assumed).
More information on this syntax [:enhancements/buffer:can be found here]. More information on this syntax [:enhancements/buffer:can be found here].
Showing the changes needed to produce :file:`convolve3.pyx` only: Showing the changes needed to produce :file:`convolve3.pyx` only::
.. sourcecode:: cython
... ...
def naive_convolve(np.ndarray[DTYPE_t, ndim=2] f, np.ndarray[DTYPE_t, ndim=2] g): def naive_convolve(np.ndarray[DTYPE_t, ndim=2] f, np.ndarray[DTYPE_t, ndim=2] g):
...@@ -374,9 +372,7 @@ The array lookups are still slowed down by two factors: ...@@ -374,9 +372,7 @@ The array lookups are still slowed down by two factors:
2. Negative indices are checked for and handled correctly. The code above is 2. Negative indices are checked for and handled correctly. The code above is
explicitly coded so that it doesn't use negative indices, and it explicitly coded so that it doesn't use negative indices, and it
(hopefully) always access within bounds. We can add a decorator to disable (hopefully) always access within bounds. We can add a decorator to disable
bounds checking: bounds checking::
.. sourcecode:: cython
... ...
cimport cython cimport cython
...@@ -395,9 +391,7 @@ positive, by casting the variables to unsigned integer types (if you do have ...@@ -395,9 +391,7 @@ positive, by casting the variables to unsigned integer types (if you do have
negative values, then this casting will create a very large positive value negative values, then this casting will create a very large positive value
instead and you will attempt to access out-of-bounds values). Casting is done instead and you will attempt to access out-of-bounds values). Casting is done
with a special ``<>``-syntax. The code below is changed to use either with a special ``<>``-syntax. The code below is changed to use either
unsigned ints or casting as appropriate: unsigned ints or casting as appropriate::
.. sourcecode:: cython
... ...
cdef int s, t # changed cdef int s, t # changed
...@@ -456,9 +450,7 @@ function call.) ...@@ -456,9 +450,7 @@ function call.)
More generic code More generic code
================== ==================
It would be possible to do: It would be possible to do::
.. sourcecode:: cython
def naive_convolve(object[DTYPE_t, ndim=2] f, ...): def naive_convolve(object[DTYPE_t, ndim=2] f, ...):
......
.. highlight:: cython
.. _overview-label: .. _overview-label:
******** ********
......
.. highlight:: cython
Differences between Cython and Pyrex Differences between Cython and Pyrex
==================================== ====================================
......
.. highlight:: cython
.. _sharing-declarations-label: .. _sharing-declarations-label:
Sharing Declarations Between Cython Modules Sharing Declarations Between Cython Modules
......
.. highlight:: cython
.. _compilation_label: .. _compilation_label:
**************************** ****************************
......
.. highlight:: cython
.. _tutorial_label: .. _tutorial_label:
********* *********
...@@ -107,7 +109,10 @@ Here's a small example showing some of what can be done. It's a routine for ...@@ -107,7 +109,10 @@ Here's a small example showing some of what can be done. It's a routine for
finding prime numbers. You tell it how many primes you want, and it returns finding prime numbers. You tell it how many primes you want, and it returns
them as a Python list. them as a Python list.
:file:`primes.pyx`: :: :file:`primes.pyx`:
.. sourcecode:: cython
:linenos:
def primes(int kmax): def primes(int kmax):
cdef int n, k, i cdef int n, k, i
......
.. highlight:: cython
.. _wrapping-cplusplus-label: .. _wrapping-cplusplus-label:
Wrapping C++ Classes in Cython Wrapping C++ Classes in Cython
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment