Book Image

Learning Cython Programming (Second Edition) - Second Edition

By : Philip Herron
Book Image

Learning Cython Programming (Second Edition) - Second Edition

By: Philip Herron

Overview of this book

Cython is a hybrid programming language used to write C extensions for Python language. Combining the practicality of Python and speed and ease of the C language it’s an exciting language worth learning if you want to build fast applications with ease. This new edition of Learning Cython Programming shows you how to get started, taking you through the fundamentals so you can begin to experience its unique powers. You’ll find out how to get set up, before exploring the relationship between Python and Cython. You’ll also look at debugging Cython, before moving on to C++ constructs, Caveat on C++ usage, Python threading and GIL in Cython. Finally, you’ll learn object initialization and compile time, and gain a deeper insight into Python 3, which will help you not only become a confident Cython developer, but a much more fluent Python developer too.
Table of Contents (14 chapters)
Learning Cython Programming Second Edition
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Cython and NumPy


NumPy is a scientific library designed to provide functionality similar to or on par with MATLAB, which is a paid proprietary mathematics package. NumPy has a lot of popularity with Cython users since you can seek out more performance from your highly computational code using C types. In Cython, you can import this library as follows:

import numpy as np
cimport numpy as np

np.import_array()

You can access full Python APIs as follows:

np.PyArray_ITER_NOTDONE

So, you can integrate with iterators at a very native area of the API. This allows NumPy users to get a lot of speed when working with native types via something as follows:

cdef double * val = (<double*>np.PyArray_MultiIter_DATA(it, 0))[0]

We can cast the data from the array to double, and it's a cdef type in Cython to work with now. For more information and NumPy tutorials, visit https://github.com/cython/cython/wiki/tutorials-numpy.