Cython Cheat Sheet Cython ( Is a C Extension to the Python Programming Language That Allows for Static Compi- Lation and Strong Type Declaration

Cython Cheat Sheet Cython ( Is a C Extension to the Python Programming Language That Allows for Static Compi- Lation and Strong Type Declaration

Cython Cheat Sheet Cython (https://cython.org/) is a C extension to the Python programming language that allows for static compi- lation and strong type declaration. It’s aim is to speed up Python significantly. Compiling Cython Class and function declaration Jupyter notebooks (temporary) Classes: %load_ext cython cdef class import Cython TestClass(object): # making variables accessible from Python # write code using cython # using public: # run cell cdef public int # nb! print functions have to be used a,b cdef public double # %%cython has to be written c # to each cell using it def # %%cython -a outputs function documentation __init__(self): # (which parts are compiled to C self.a=0 # and which are Python) self.b=1 self.c= 3.14 %% cython Functions: cdef int multiplyWithOne(int x): x*=1; def functionExample(int x, float y): return x # function is callable from Python and Cython print(multiplyWithOne(9)) return y*x ... pyximport (temporary) cdef functionExample(int x, float y): 1. write a file.pyx containing the source code # function is callable from Cython only 2. import (and autocompile) file.pyx using pyximport: # function is optimized return y*x # using pyximport for auto-compilation import pyximport cpdef functionExample(int x, float y): pyximport.install() # function is callable from Cython (optimized) import file #imports file.pyx # function is callable from Python (unoptimized) setup.py script return y*x 1. write a file.pyx containing the source code 2. compile it using a setup.py file containing: Further, the return type can be added to the function: from distutils.core import setup from Cython.Build import cythonize cdef float functionExample(int x, float y): # function is optimized & callable from Cython only setup(ext_modules= cythonize( ``file.pyx'')) return y*x 3. compile it: $ python setupy.py build_ext --inplace Type declaration Files .pyx Source code containing Cython code Cython offers static typing of the following types: .c C source code generated from .pyx file int A = 1 integer .so shared object file from compilation float A = 1.0 floating point (fp) .html documentation from %%cython -a double A = 1.0 double precision fp ./build/ temporary files from compilation char *A = ’1’ string (pointer (*)!!) Py_ssize_t signed integer for NumPy indices Misc Memory management Calling external C functions cdef extern from ''file.h'': Manual memory management can be done by importing double someFunctionsName(double) memory handling functions from the C standard library: from libc.math cimport cos void* malloc cdef double functionName(double x) void* realloc return cos(x) void free ©Simon Wenkel (https://www.simonwenkel.com) This pdf is licensed under the CC BY-SA 4.0 license..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    1 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us