Skip to content

Commit c4e8904

Browse files
committed
Cython formatting fixes etc
1 parent e561111 commit c4e8904

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

content/cython.rst

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ the good programmer productivity of Python together with the high performance
9797
of C. Cython also makes it easy to interact with external C/C++ code.
9898

9999
The Cython compiler processes code written in Python, or more
100-
commonly the Cython extension of Python language, turns it into valid C-code
101-
which is then compiled into a Python extension module using a C compiler
102-
(GCC, Clang, MSVC, ...). The Cython programming language is a a superset of
103-
Python that adds C-like static type declarations and other features that
104-
make it possible to generate efficient machine code.
100+
commonly the Cython extension of Python language, and turns it into valid
101+
C-code which is then compiled into a Python extension module using a
102+
C compiler (GCC, Clang, MSVC, ...). The Cython programming language is a
103+
superset of Python that adds C-like static type declarations and other
104+
features that make it possible to generate efficient machine code.
105105

106106
.. callout::
107107

@@ -125,7 +125,7 @@ Suppose we have a Python module called **my_module.py** that contains:
125125
126126
Cython allows one to compile **my_module.py** directly to machine code while
127127
still allowing its contents to be imported and used from Python code. We can
128-
Cynthonize the module "manually" from command line:
128+
Cythonize the module "manually" from command line:
129129

130130
.. code:: bash
131131
@@ -179,7 +179,7 @@ We first load the Cython extension, e.g. in the very first cell: ::
179179

180180
%load_ext Cython
181181

182-
We can Cythonize cell contents using the magic `%%cython` and executing it:
182+
We can Cythonize cell contents using the magic `%%cython`:
183183

184184
.. code:: python
185185
@@ -189,6 +189,7 @@ We can Cythonize cell contents using the magic `%%cython` and executing it:
189189
return result
190190
191191
192+
The compiled function can then be called from other cells.
192193

193194
There is also `%%cython --annotate` which is useful for analyzing the
194195
generated C code.
@@ -198,10 +199,10 @@ Adding static type information
198199
------------------------------
199200

200201
So far our Cythonized extension module is rather dumb. We have reduced some
201-
of interpreting overhead by compiling the code, but it's still using Python's
202+
of the interpreting overhead by compiling the code, but it's still using Python's
202203
fully dynamic type system with the same boxing and unboxing overhead as in
203204
standard Python. This is because there are no type declarations in the code
204-
that Cython could use for optimizations.
205+
that Cython could use to optimize.
205206

206207
When Cythonizing a Python code, static type information can be added
207208
either:
@@ -211,7 +212,7 @@ either:
211212
- By declaring variables with the **cdef** Cython keyword, followed by
212213
the the type.
213214

214-
To make Cythonize a function that adds two integers and returns the result as
215+
To make Cython function that adds two integers and returns the result as
215216
an integer, we would write:
216217

217218
.. code:: python
@@ -222,7 +223,7 @@ an integer, we would write:
222223
return result
223224
224225
The function works now only with integers but with less boxing/unboxing
225-
overheads. Store this as **my_module.pyx** (note the file extension) and
226+
overhead. Store this as **my_module.pyx** (note the file extension) and
226227
Cythonize as before:
227228

228229
.. code:: bash
@@ -303,8 +304,8 @@ declare the array's datatype and dimensions:
303304

304305
.. code:: python
305306
306-
import numpy as np # Normal NumPy import
307-
cimport numpy as cnp # Import for NumPY C-API
307+
import numpy as np # Normal Numpy import
308+
cimport numpy as cnp # Import for Numpy C-API
308309
309310
def fast_looper(int N):
310311
""""""
@@ -330,8 +331,8 @@ pure Python implementation!
330331
`cimport numpy` needs access to Numpy C-headers which are usually included
331332
in Python distributions. This usually works out of the box for Jupyter
332333
notebooks. However, if using the command line `cythonize` tool you may need
333-
to manually set include paths for the C compiler knows where to find the
334-
headers. Refer to `the docs <https://cython.readthedocs.io/en/latest/src/userguide/numpy_tutorial.html#compilation>`__
334+
to manually set include paths for the C compiler.
335+
Refer to `the docs <https://cython.readthedocs.io/en/latest/src/userguide/numpy_tutorial.html#compilation>`__
335336
for more details.
336337

337338
.. callout::
@@ -347,8 +348,8 @@ More Numpy indexing enhancements
347348
When indexing arrays, Numpy does some bounds checking in an attempt to catch
348349
logic errors (e.g. attempting to access element at index 100 of an array of
349350
length 10). Numpy also checks for negative indices to support wraparound
350-
syntax like **a[-1]**. We can tell Cython to disable these checks for some extra
351-
performance:
351+
syntax like **a[-1]**. We can tell Cython to disable these checks for some
352+
extra performance:
352353

353354
.. code:: python
354355
@@ -395,9 +396,9 @@ TODO: when to use other C extension stuff
395396
Further reading
396397
---------------
397398

398-
- Newer usage of Numpy arrays (memory views) https://cython.readthedocs.io/en/latest/src/userguide/numpy_tutorial.html#numpy-tutorial
399-
- TODO
400-
399+
- Cython `memory views <https://cython.readthedocs.io/en/latest/src/userguide/memoryviews.html>`__
400+
are a newer and more general way of interfacing with Numpy arrays and other buffer-like objects.
401+
- `Calling C functions from Cython <https://cython.readthedocs.io/en/latest/src/tutorial/external.html>`__
401402

402403
Summary
403404
-------
@@ -407,4 +408,4 @@ Summary
407408
Acknowledgements
408409
----------------
409410

410-
This material has been adapted from the "Python for HPC" course by CSC - IT Center for Science.
411+
This material has been adapted from the "Python for HPC" course by CSC - IT Center for Science.

0 commit comments

Comments
 (0)