31,19 €
Learn the fundamentals of Cython to extend the legacy of your applications
This book is for developers who are familiar with the basics of C and Python programming and wish to learn Cython programming to extend their applications.
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.
This practical and a fast-paced guide gives you all the information you need to start programming using Cython.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 109
Veröffentlichungsjahr: 2016
Copyright © 2016 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2013
Second Edition: February 2016
Production reference: 1160216
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78355-167-5
www.packtpub.com
Author
Philip Herron
Reviewer
Namit Kewat
Commissioning Editor
Priya Singh
Acquisition Editor
Indrajit Das
Content Development Editor
Priyanka Mehta
Technical Editor
Murtaza Tinwala
Copy Editor
Yesha Gangani
Project Coordinator
Izzat Contractor
Proofreader
Safis Editing
Indexer
Tejal Daruwale Soni
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
Philip Herron is a developer who focuses his passion toward compilers and virtual machine implementations. When he was first accepted to Google Summer of Code 2010, he used inspiration from Paul Biggar's PhD on the optimization of dynamic languages to develop a proof of the concept GCC frontend to compile Python. This project sparked his deep interest in how Python works.
After completing a consecutive year on the same project in 2011, Philip applied to Cython under the Python foundation to gain a deeper appreciation of the standard Python implementation. Through this he started leveraging the advantages of Python to control the logic in systems or even add more high-level interfaces, such as embedding Flask web servers in a REST API to a system-level piece of software, without writing any C code.
Philip currently works as a software consultant for Instil Software based in Northern Ireland. He develops mobile applications with embedded native code for video streaming. Instil has given him a lot of support in becoming a better engineer.
He has written several tutorials for the UK-based Linux Format magazine on Python and loves to share his passion for the Python programming language.
To achieve writing this book, I would like to thank many people—my partner, Kirsty Johnston, for putting up with my late nights and giving me the confidence I needed; she is the best! I would like to thank my mum and dad, who have always supported me my whole life—thanks for helping me so much. Ian Lance Taylor, my mentor, from Google Summer of Code deserves a special mention. If it wasn't for him, I wouldn't be writing anything like this right now. I would like to thank Robert Bradshaw for mentoring my Cython Auto-PXD project; even though I had a lot going on at the time, he helped me get it done and pass. Special thanks to Nicholas Marriott for helping me with the Tmux code base. I would also like to thank Gordon Hamilton and Dr. Colin Turner for the all the support they have given me. Finally, I would like to thank Tara Simpson, Chris Van Es, Niall Kelly, and Matt McComb for the mentoring they have given me at Instil.
Namit Kewat is a software engineer working in Ahmedabad, India. He has expertise in developing high performance web applications. He also specializes in performing data analysis for generating financial information (XBRL). He is a keen learner and always looks forward to applying his skills for solving complex problems. This unique approach of his is paying him dividends both internally and globally. Namit is also fond of blogging. Those interested can visit his blog at http://namitkewat.github.io/
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
Cython is a tool that makes writing native extensions to Python as easy as writing them in Python. For those who are unaware, you can implement Python modules as pure the C code, which will, for all intents and purposes, look and act like any Python code. This is required when implementing modules in Python, such as the built-in zip module which use native zlib under the hood. Doing this makes sense for the standard library modules part of Python, though for most users of Python, writing native modules should be the last course of action if possible.
Writing native modules is hard and requires prerequisite knowledge of how to use the garbage collector calls correctly in order to avoid memory leaks. It also requires an understanding of how the GIL is used, which changes if you are using CPython or PyPy. It also requires knowledge of the module structures and argument passing internally to the Python runtime. Therefore, it isn't a trivial process when the need arises. Cython lets us write and work with the native code without having to know anything about the Python runtime. We can write almost pure Python code that just so happens to let us manipulate C/C++ types and classes. We can call back and forth from the native code and into the Python code.
More importantly, Cython removes the complexity and intrinsicity and lets the programmer focus on solving problems.
Chapter 1, Cython Won't Bite, introduces core concepts and demonstrates Cython "Hello World". It discusses the typing and type conversion.
Chapter 2, Understanding Cython, acts as a reference throughout the book. We look at custom C types and function pointers. Using this, we will successfully use Python modules directly from C code.
Chapter 3, Extending Applications, uses everything from the previous chapters to write native Tmux commands using Python instead of C/C++.
Chapter 4, Debugging Cython, uses the cygdb wrapper over gdb to debug Cython code.
Chapter 5, Advanced Cython, introduces how well Cython can work with C++ classes and templates. In general, it also covers caveats in Cython.
Chapter 6, Further Reading, briefly looks at the related projects and interesting sources of new learning.
For this book, I used my MacBook and an Ubuntu virtual machine (GDB is too old on Mac OS X for debugging). You will require the following on Mac OS X:
On Ubuntu/Debian you can install everything via the following command:
I will go over this in the introduction, but as long as you have a working C compiler and Python along with Python libraries and headers installed, you will have everything you need for Cython.
This book is intended for C/C++ developers who like using Python and Python users wanting to implement native C/C++ extensions to Python. As a reader, you can expect to be shown how you can develop applications with Cython with an emphasis on extending existing systems and with help on how you can approach it.
Extending legacy systems can be difficult, but the rewards can be great. Consider low-level thread-aware or I/O-sensitive operations in C and maintain the logic handled and provided by Python. This model of development can prove to be efficient and of great return to the development time, which can be particularly expensive when it comes to C applications.
It also allows for much more rapid development of the state or logic in a system. There is no need to worry about long data conversion algorithms in C to do small things and then needing to change them all again.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The preferred one would be to use pip."
A block of code is set as follows:
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
