Learning Cython Programming

Book description

Developers who like C/C++ will love the added dynamism of Cython. This practical guide gives you everything you need to start programming with Cython and enhancing your existing applications. It could change your working life.

  • Extend C applications with pure Python code
  • Expand low-level C open source projects with pure Python – Tmux
  • Get the most out of highly computational Python code using Cython
  • Integrate your C applications with Python Distutils and Automake/Autoconf

In Detail

Cython is a very powerful combination of Python and C. Using Cython, you can write Python code that calls back and forth from and to C or C++ code natively at any point. It is a language with extra syntax allowing for optional static type declarations. It is also a very popular language as it can be used for multicore programming.

Learning Cython Programming will provide you with a detailed guide to extending your native applications in pure Python; imagine embedding a twisted web server into your native application with pure Python code. You will also learn how to get your new applications up and running by reusing Python’s extensive libraries such as Logging and Config Parser to name a few.

With Learning Cython Programming, you will learn that writing your own Python module in C from scratch is not only hard, but is also unsafe. Cython will automatically handle all type-conversion issues as well as garbage collection on your code. You can also still write all your code in Python but have it compiled and called directly in C as if it was just another function or data.

This book also demonstrates how you can take the open source project Tmux and extend it to add new commands directly in pure Python. With this book, you will learn everything you need to know to get up and running with Cython and how you can reuse examples in a practical way.

Table of contents

  1. Learning Cython Programming
    1. Table of Contents
    2. Learning Cython Programming
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Cython Won't Bite
      1. What this book is
      2. What this book isn't
      3. Installing Cython
        1. The emacs mode
        2. Getting the code examples
      4. Hello World
      5. Module on your own
        1. Calling into your C code
          1. Type conversion
      6. Summary
    9. 2. Understanding Cython
      1. Cython cdef
        1. Linking models
        2. The public keyword
        3. Logging into Python
      2. Python ConfigParser
      3. Cython cdef syntax and usage reference
        1. Structs
        2. Enums
      4. Typedef and function pointers
      5. Scalable asynchronous servers
      6. C sockets with libevent
        1. What is libevent?
        2. Messaging engine
        3. Cython callbacks
        4. Cython PXD
        5. Python messaging engine
      7. Integration with build systems
        1. Python distutils
        2. GNU/Autotools
      8. Summary
    10. 3. Extending Applications
      1. Cython pure Python code
      2. Python bindings
      3. Python garbage collector
      4. Extending Tmux
        1. Tmux build system
      5. Embedding Python
      6. Cythonizing struct cmd_entry
      7. Implementing a Tmux command
      8. Hooking everything together
      9. Compiling pure Python code
      10. Summary
    11. 4. Debugging Cython
      1. Using GDB on your code
        1. Running cygdb
      2. General Cython caveats
        1. Type checking
        2. No * operator
        3. Python exceptions in C
        4. For loops on C types
        5. Bool type
        6. No C const
        7. Multiple Cython files
        8. Initializing struct
      3. Calling into pure Python modules
        1. Keeping call stacks small and pure
      4. Summary
    12. 5. Advanced Cython
      1. C++ constructs
        1. Namespaces
        2. Classes
        3. C++ new keyword and allocation
        4. Exceptions
        5. Bool type
        6. Overloading
        7. Templates
        8. Static class member attribute
      2. Caveat on C++ usage
        1. Calling in C and C++ functions
        2. Namespaces
        3. Python distutils
      3. Python threading and GIL
        1. Atomic instructions
        2. Read/write lock
        3. Cython keywords
        4. Messaging server revisited
      4. More inspiration
        1. Messaging server working with SQL
        2. Python IRC notifier
        3. Unit testing the native code
        4. Preventing subclassing
        5. Cython typing via annotations
      5. Parsing large amounts of data
      6. Summary
    13. 6. Further Reading
      1. Keyword cpdef
      2. OpenMP support
      3. Object initialization
      4. Compile time
      5. Python 3
        1. Using PyPy
        2. AutoPXD
        3. Pyrex versus Cython
        4. SWIG versus Cython
        5. Cython and NumPy
        6. Numba versus Cython
        7. Parakeet and Numba
        8. GCCPy Python frontend to GCC
        9. Links and further reading
      6. Summary
    14. Index

Product information

  • Title: Learning Cython Programming
  • Author(s):
  • Release date: September 2013
  • Publisher(s): Packt Publishing
  • ISBN: 9781783280797