Learning Cython Programming - Second Edition
()
About this ebook
About This Book
- Learn how to extend C applications with pure Python code
- Get more from Python – you’ll not only learn Cython, you’ll also unlock a greater understanding of how to harness Python
- Packed with tips and tricks that make Cython look easy, dive into this accessible programming guide and find out what happens when you bring C and Python together!
Who This Book Is For
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.
What You Will Learn
- Reuse Python logging in C
- Make an IRC bot out of your C application
- Extend an application so you have a web server for rest calls
- Practice Cython against your C++ code
- Discover tricks to work with Python ConfigParser in C
- Create Python bindings for native libraries
- Find out about threading and concurrency related to GIL
- Expand Terminal Multiplexer Tmux with Cython
In Detail
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.
Style and approach
This practical and a fast-paced guide gives you all the information you need to start programming using Cython.
Related to Learning Cython Programming - Second Edition
Related ebooks
Expert Python Programming - Third Edition: Become a master in Python by learning coding best practices and advanced programming concepts in Python 3.7, 3rd Edition Rating: 0 out of 5 stars0 ratingsModular Programming with Python Rating: 0 out of 5 stars0 ratingsPython 3 Object-oriented Programming - Second Edition Rating: 4 out of 5 stars4/5Distributed Computing with Python Rating: 0 out of 5 stars0 ratingsNumPy Essentials Rating: 0 out of 5 stars0 ratingsMastering Flask Rating: 0 out of 5 stars0 ratingsMastering Python High Performance Rating: 0 out of 5 stars0 ratingsFlask Blueprints Rating: 0 out of 5 stars0 ratingsMastering Python Regular Expressions Rating: 5 out of 5 stars5/5Python GUI Programming Cookbook - Second Edition Rating: 5 out of 5 stars5/5Mastering Python Design Patterns Rating: 0 out of 5 stars0 ratingsPython Data Structures and Algorithms Rating: 5 out of 5 stars5/5Mastering PyCharm Rating: 5 out of 5 stars5/5Python for Secret Agents - Volume II Rating: 0 out of 5 stars0 ratingsBuilding Web Applications with Flask Rating: 0 out of 5 stars0 ratingsLearning Python Design Patterns - Second Edition Rating: 0 out of 5 stars0 ratingsReinforcement Learning Algorithms with Python: Learn, understand, and develop smart algorithms for addressing AI challenges Rating: 0 out of 5 stars0 ratingsBoost.Asio C++ Network Programming - Second Edition Rating: 0 out of 5 stars0 ratingsPython High Performance - Second Edition Rating: 0 out of 5 stars0 ratingsQt 5 Blueprints Rating: 4 out of 5 stars4/5C++ Windows Programming Rating: 0 out of 5 stars0 ratingsLearning Python Rating: 5 out of 5 stars5/5Python Tools for Visual Studio Rating: 0 out of 5 stars0 ratingsMastering Python Networking Rating: 5 out of 5 stars5/5Python Web Scraping - Second Edition Rating: 5 out of 5 stars5/5PySide GUI Application Development - Second Edition Rating: 0 out of 5 stars0 ratingsLearning Apache Thrift Rating: 0 out of 5 stars0 ratings
Programming For You
HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Python Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsLearn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 0 out of 5 stars0 ratingsJava for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5The Unofficial Guide to Open Broadcaster Software: OBS: The World's Most Popular Free Live-Streaming Application Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Hacking: Ultimate Beginner's Guide for Computer Hacking in 2018 and Beyond: Hacking in 2018, #1 Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5The Little SAS Book: A Primer, Sixth Edition Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Teach Yourself C++ Rating: 4 out of 5 stars4/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5
Reviews for Learning Cython Programming - Second Edition
0 ratings0 reviews
Book preview
Learning Cython Programming - Second Edition - Herron Philip
Table of Contents
Learning Cython Programming Second Edition
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Cython Won't Bite
Installing Cython
Emacs mode
Getting the code examples
Getting started – Hello World
Using distutils with Cython
Calling C functions from Python
Type conversion in Cython
Summary
2. Understanding Cython
Linking models
Cython keyword – cdef
Structs
Enums
Typedef and function pointers
The public keyword
Keyword cpdef
Logging from C/C++ into Python
Using Python ConfigParser from C/C++
Callbacks from Python to C/C++
Cython PXD
Integration with build systems
Python Distutils
GNU/Autotools
Summary
3. Extending Applications
Cython pure Python code
Compiling pure Python code
Avoid Makefiles – pyximport
Python garbage collector
Extending Tmux
Tmux build system
Embedding Python
Cythonizing struct cmd_entry
Implementing a Tmux command
Hooking everything together
Summary
4. Debugging Cython
Using GDB with Cython
Running cygdb
Cython caveats
Type checking
Dereference operator (*)
Python exceptions
C/C++ iterators
Boolean error
Const keyword
Multiple Cython inputs
Struct initialization
Calling into pure Python modules
Summary
5. Advanced Cython
Cython and C++
Namespaces
Classes
C++ new and del keyword
Exceptions
Bool type
Overloading
Templates
Static class member attribute
Calling C++ functions – Caveat
Namespaces – Caveat
Python distutils
Python threading and GIL
Atomic instructions
Read/write lock
Cython keywords
Messaging server
Caveat on GIL
Unit testing the native code
Preventing subclassing
Parsing large amounts of data
Summary
6. Further Reading
Overview
OpenMP support
Compile time preprocessor
Python 3
PyPy
AutoPXD
Pyrex and Cython
SWIG and Cython
Cython and NumPy
Numba versus Cython
Parakeet and Numba
Relevant Links
Summary
Index
Learning Cython Programming Second Edition
Learning Cython Programming Second Edition
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
Credits
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
About the Author
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.
Acknowledgments
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.
About the Reviewer
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/
www.PacktPub.com
eBooks, discount offers, and more
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
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.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Preface
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