Skip to main content

Cython performed inline: compile and run your Cython snippets on the fly.

Project description

cyper

Cython performed inline: compile and run your Cython snippets on the fly.

It is aiming to provide a flexible alternative to the IPython Cython magic command outside the IPython/Jupyter environment.

Features

  • Simple, get rid of the tedious setup files or makefile.
  • Smart, auto-detect and set compiler flags for Numpy and OpenMP.
  • Highly customizable, allow one to set Cython directives, environment variables and compiler options.

Usage

Basic usage

  • Basic usage

    import cyper
    code = r'''
    def func(x):
        return 2.0 * x
    '''
    pyx = cyper.inline(code)
    pyx.func(1)
    # 2.0
    

    Raw string is recommended to avoid breaking escape character.

  • It is convenient (though usually not encouraged) to export the variables from compiled module to the current namespace

    cyper.inline(code, globals())
    func(1)
    
  • Example of using Numpy array and external gsl library, assuming gsl installed at /opt/gsl/

    code = r'''
    import numpy as np
    
    cdef extern from "gsl/gsl_math.h":
        double gsl_pow_int (double x, int n)
    
    def pow(double x, int n):
        y = gsl_pow_int(x, n)
        return y
    
    def pow_array(double[:] x, int n):
        cdef:
            int i, m=len(x)
            double[:] y=np.empty(m, dtype='f8')
        for i in range(m):
            y[i] = gsl_pow_int(x[i], n)
        return y.base
    '''
    pyx = cyper.inline(
        code,
        include_dirs=['/opt/gsl/include/'],
        library_dirs=['/opt/gsl/lib'],
        libraries=['gsl', 'gslcblas']
    )
    
    pyx.pow(2, 6)
    # 64.0
    
    import numpy as np
    pyx.pow_array(np.arange(5, dtype='f8'), 2)
    # array([ 0.,  1.,  4.,  9., 16.])
    
  • Get better performance (at your own risk) with arrays

    cyper.inline(code, fast_indexing=True)
    # or equivalently
    cyper.inline(code, directives=dict(boundscheck=False, wraparound=False))
    

Advanced usage

  • Set the compiler options, e.g., compiling OpenMP codes with gcc

    cyper.inline(openmpcode,
                 extra_compile_args=['-fopenmp'],
                 extra_link_args=['-fopenmp'],
                 )
    # use '-openmp' or '-qopenmp' (>=15.0) for Intel
    # use '/openmp' for Microsoft Visual C++ Compiler
    # use '-fopenmp=libomp' for Clang
    

    Or equivalently write this for short

    cyper.inline(openmpcode, openmp='-fopenmp')
    

    The cython directives and distutils extension_args can also be set in a directive comment at the top of the code snippet, e.g.,

    code = r'''
    # cython: boundscheck=False, wraparound=False, cdivision=True
    # distutils: extra_compile_args = -fopenmp
    # distutils: extra_link_args = -fopenmp
    ...<code>...
    '''
    cyper.inline(code)
    
  • Set environment variables, e.g., using icc to compile

    cyper.inline(code, environ={'CC':'icc', 'LDSHARED':'icc -shared'})
    

    See https://software.intel.com/en-us/articles/thread-parallelism-in-cython

  • Set directory for searching cimport (.pxd file)

    cyper.inline(code, cimport_dirs=[custom_path]})
    # or equivalently
    cyper.inline(code, cythonize_args={'include_path': [custom_path]})
    

    Try setting cimport_dirs=sys.path if Cython can not find the installed cimport modules.

Installation

  • Dependencies: Cython

  • Installation:

    pip install cyper
    

License

Cyper is licensed under the MIT License

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cyper-1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

cyper-1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file cyper-1.0.tar.gz.

File metadata

  • Download URL: cyper-1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for cyper-1.0.tar.gz
Algorithm Hash digest
SHA256 404ff23055a0a4dcec85495274515e9382ed090fb01f42aa71901bd499b434ea
MD5 4bb8fadaa236486d98d653e349b34c56
BLAKE2b-256 8289794300e3f911cdb8a7a78e8e5281edb0c80fa40825099816a32409701127

See more details on using hashes here.

File details

Details for the file cyper-1.0-py3-none-any.whl.

File metadata

  • Download URL: cyper-1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for cyper-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a8af434d36e2844e4af5530ddc0ed5078dff15a73d1119de695b2603c69531b5
MD5 9368b2da14d4fd55ffb92e06c612e4ff
BLAKE2b-256 e1ee07fde704603bc896d69057d6f1ba201b470095ae8ad56a41cb831c237094

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page