Skip to main content

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

Metadata

Release files for cyper 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cyper 1.0
File Size Uploaded Provenance Actions
cyper-1.0.tar.gz 7.7 kB Without provenance Details for cyper-1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cyper 1.0
File Interpreter ABI Platform Actions
cyper-1.0-py3-none-any.whl Python 3 none any Details for cyper-1.0-py3-none-any.whl

Total release size: 16.8 kB

Release files / cyper-1.0.tar.gz

Download URL cyper-1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
404ff23055a0a4dcec85495274515e9382ed090fb01f42aa71901bd499b434ea
BLAKE2b-256 checksum
How to use checksums
8289794300e3f911cdb8a7a78e8e5281edb0c80fa40825099816a32409701127
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / cyper-1.0-py3-none-any.whl

Download URL cyper-1.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8af434d36e2844e4af5530ddc0ed5078dff15a73d1119de695b2603c69531b5
BLAKE2b-256 checksum
How to use checksums
e1ee07fde704603bc896d69057d6f1ba201b470095ae8ad56a41cb831c237094
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for cyper 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page