Cython: Realtime compile_and_load (.pyx)#

An example showing the cython submodule..

# Authors: The scikit-plots developers
# SPDX-License-Identifier: BSD-3-Clause

cython examples#

import scikitplot as sp
sp.__version_iso_8601__
'2026.02.01'
from scikitplot import cython

print(cython.__doc__)
A lightweight runtime Cython development kit with caching, pinning,
garbage collection, and templating support.

:mod:`scikitplot.cython` enables real-time, in-place (in-situ) generation of
low-level Cython packages and modules for immediate use and testing.

.. seealso::
   * https://doc.sagemath.org/html/en/reference/misc/sage/misc/cython.html
   * https://github.com/cython/cython
   * https://cython.readthedocs.io/en/latest/index.html
from scikitplot.cython import compile_and_load
m = compile_and_load("def f(int n):\n    return n*n")
m.f(10)
100
['basic_cython/t01_square_int', 'basic_cython/t02_add_float', 'basic_cython/t03_toggle_bool', 'basic_cython/t04_repeat_str', 'basic_cython/t05_int_overflow_demo', 'basic_cython/t06_xor_bytes', 'complex_cython/t01_cpp_vector_sum', 'complex_cython/t01_fast_clip', 'complex_cython/t02_nogil_sum', 'complex_cython/t03_nogil_sum', 'complex_cython/t04_nogil_sum', 'complex_cython/t05_popcount64', 'devel_cython/t01_directives', 'devel_cython/t02_cdef_class_counter', 'devel_cython/t03_cdef_class_counter', 'devel_cython/t04_cdef_class_counter', 'devel_cython/t05_cdef_class_counter', 'devel_numcpp_cpp_api_cython/t01_cpp_language', 'devel_numcpp_cpp_api_cython/t01_cpp_memoryview', 'devel_numcpp_cpp_api_cython/t01_numpy_cpp_memoryview', 'devel_numcpp_cpp_api_cython/t02_cpp_vector_size', 'devel_numcpp_cpp_api_cython/t03_cpp_vector_size', 'devel_numpy_c_api_cython/t01_numpy_ndarray_sum', 'devel_numpy_c_api_cython/t01_numpy_typed_ndarray', 'devel_numpy_c_api_cython/t02_numpy_sum_1d', 'devel_numpy_c_api_cython/t03_numpy_sum_1d', 'devel_numpy_c_api_cython/t04_numpy_sum_1d', 'easy_cython/t01_memoryview_sum', 'easy_cython/t01_sum_memoryview', 'easy_cython/t01_sum_view', 'easy_cython/t04_vector_dot_list', 'easy_cython/t05_vector_scale_memview', 'easy_cython/t06_vector_norm2_memview', 'easy_cython/t07_vector_add_list', 'easy_cython/t08_parse_int_ascii', 'hard_cython/t01_cdef_class_counter', 'hard_cython/t01_cdef_class_vector', 'hard_cython/t01_dot_product', 'hard_cython/t02_malloc_fill', 'hard_cython/t03_malloc_fill', 'hard_cython/t04_struct_point_distance', 'medium_cython/t01_fused_add', 'medium_cython/t01_fused_types', 'medium_cython/t01_running_mean', 'medium_cython/t02_fused_add', 'medium_cython/t03_fused_add', 'medium_cython/t04_minmax_memview', 'medium_cython/t05_argmax_memview', 'mixed/t01_square_int', 'mixed/t02_fib_cpdef', 'mixed/t03_cdef_class_counter', 'mixed/t04_memoryview_sum', 'mixed/t05_numpy_ndarray_sum', 'mixed/t06_directives_boundscheck', 'mixed/t07_libc_math', 'mixed/t08_struct_point', 'mixed/t09_enum_state', 'mixed/t10_safe_div_except', 'mixed/t11_fused_types_dot', 'mixed/t12_inline_clamp', 'mixed/t13_bytes_xor', 'mixed/t14_string_reverse', 'mixed/t15_lcg_rng', 'mixed/t16_insertion_sort', 'mixed/t17_kahan_sum', 'mixed/t18_histogram_int', 'mixed/t19_popcount', 'mixed/t20_matmul_small', 'module_cython/t01_counter', 'module_cython/t01_extension_class', 'module_cython/t01_module_constants', 'module_cython/t02_cdef_class_counter', 'module_cython/t03_cdef_class_counter', 'module_cython/t90_include_pxi_square', 'module_cython/t91_extern_header_add']
PosixPath('/home/circleci/.pyenv/versions/3.11.14/lib/python3.11/site-packages/scikitplot/cython/_templates/basic_cython/t01_square_int.pyx')
# cython: language_level=3
"""Basic Cython template: typed function.

This demonstrates a minimal compiled function with C integer typing.
"""


def square(int n):
    """Return n*n using C integer arithmetic."""
    return n * n
PosixPath('/home/circleci/.cache/scikitplot/cython')
[CacheEntry(key='0168cdebb518cec1012769ec306530b313481ce4103cab7a696eedf8e56011c7', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/0168cdebb518cec1012769ec306530b313481ce4103cab7a696eedf8e56011c7'), module_name='scikitplot_cython_0168cdebb518cec1', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/0168cdebb518cec1012769ec306530b313481ce4103cab7a696eedf8e56011c7/scikitplot_cython_0168cdebb518cec1.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:55Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='093729e666d9308a2ee01f667576da3e7022aaf802855405f2fe6727629b9a2a', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/093729e666d9308a2ee01f667576da3e7022aaf802855405f2fe6727629b9a2a'), module_name='scikitplot_cython_093729e666d9308a', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/093729e666d9308a2ee01f667576da3e7022aaf802855405f2fe6727629b9a2a/scikitplot_cython_093729e666d9308a.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:52Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='106892295832cfc7f01dbb65bd06b364352040aef37075f2634c508019b279d1', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/106892295832cfc7f01dbb65bd06b364352040aef37075f2634c508019b279d1'), module_name='scikitplot_cython_106892295832cfc7', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/106892295832cfc7f01dbb65bd06b364352040aef37075f2634c508019b279d1/scikitplot_cython_106892295832cfc7.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:53Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='36acf5a34e6493eb1ba90b4ee0a7f2df61b5679a8201298adcd79cefe0e0aaf7', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/36acf5a34e6493eb1ba90b4ee0a7f2df61b5679a8201298adcd79cefe0e0aaf7'), module_name='wf_ext_square', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/36acf5a34e6493eb1ba90b4ee0a7f2df61b5679a8201298adcd79cefe0e0aaf7/wf_ext_square.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:59Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='3889c61dfb4a9e001e7b3ca5bda33aaad7c590480d781d1a3dcb5c1e58aa8672', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/3889c61dfb4a9e001e7b3ca5bda33aaad7c590480d781d1a3dcb5c1e58aa8672'), module_name='cpp_mode_demo', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/3889c61dfb4a9e001e7b3ca5bda33aaad7c590480d781d1a3dcb5c1e58aa8672/cpp_mode_demo.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:58Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='632e06e9a1a7a87769ae8f9e448b50fa2865f5211a9c7fac72db477b95832e8d', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/632e06e9a1a7a87769ae8f9e448b50fa2865f5211a9c7fac72db477b95832e8d'), module_name='scikitplot_cython_632e06e9a1a7a877', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/632e06e9a1a7a87769ae8f9e448b50fa2865f5211a9c7fac72db477b95832e8d/scikitplot_cython_632e06e9a1a7a877.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:52Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='7369d2e4b59fdddd1c04725aad38a69ae5e5100cbba96a6ca796f8076016103c', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/7369d2e4b59fdddd1c04725aad38a69ae5e5100cbba96a6ca796f8076016103c'), module_name='multifile_demo', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/7369d2e4b59fdddd1c04725aad38a69ae5e5100cbba96a6ca796f8076016103c/multifile_demo.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:56Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='845d660f400d30825fcddf6775bf8f257d018f58bbda5f12092a3fbdde4bc2a5', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/845d660f400d30825fcddf6775bf8f257d018f58bbda5f12092a3fbdde4bc2a5'), module_name='scikitplot_cython_845d660f400d3082', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/845d660f400d30825fcddf6775bf8f257d018f58bbda5f12092a3fbdde4bc2a5/scikitplot_cython_845d660f400d3082.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:54Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='a5d91a9aca8a988403003d9feb13944b8dbca7eb8f0ac8a20b86cef8a1ac9a94', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/a5d91a9aca8a988403003d9feb13944b8dbca7eb8f0ac8a20b86cef8a1ac9a94'), module_name='memview_scale_demo', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/a5d91a9aca8a988403003d9feb13944b8dbca7eb8f0ac8a20b86cef8a1ac9a94/memview_scale_demo.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:34:00Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': None, 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='b89f3272eca6106c5e5079f8fd5b949ad4898c3afd6de5904de4096323a5e9fb', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/b89f3272eca6106c5e5079f8fd5b949ad4898c3afd6de5904de4096323a5e9fb'), module_name='scikitplot_cython_b89f3272eca6106c', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/b89f3272eca6106c5e5079f8fd5b949ad4898c3afd6de5904de4096323a5e9fb/scikitplot_cython_b89f3272eca6106c.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:54Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d'), module_name='scikitplot_cython_c71820fcbdd3d3be', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d/scikitplot_cython_c71820fcbdd3d3be.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:33:52Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'}), CacheEntry(key='fec69e8f86415e044deb144b3819b9640ea72e345c42e71d366516bacfc57377', build_dir=PosixPath('/home/circleci/.cache/scikitplot/cython/fec69e8f86415e044deb144b3819b9640ea72e345c42e71d366516bacfc57377'), module_name='scikitplot_cython_fec69e8f86415e04', artifact_path=PosixPath('/home/circleci/.cache/scikitplot/cython/fec69e8f86415e044deb144b3819b9640ea72e345c42e71d366516bacfc57377/scikitplot_cython_fec69e8f86415e04.cpython-311-x86_64-linux-gnu.so'), created_utc='2026-02-01T06:34:01Z', fingerprint={'abi': '', 'cython': '3.2.4', 'machine': 'x86_64', 'numpy': '2.4.2', 'platform': 'Linux-6.8.0-1040-aws-x86_64-with-glibc2.35', 'processor': 'x86_64', 'python': '3.11.14', 'python_impl': 'CPython'})]
cython.list_cached()[0].build_dir
PosixPath('/home/circleci/.cache/scikitplot/cython/0168cdebb518cec1012769ec306530b313481ce4103cab7a696eedf8e56011c7')
cython.list_cached()[0].artifact_path
PosixPath('/home/circleci/.cache/scikitplot/cython/0168cdebb518cec1012769ec306530b313481ce4103cab7a696eedf8e56011c7/scikitplot_cython_0168cdebb518cec1.cpython-311-x86_64-linux-gnu.so')
144
m = compile_template('module_cython/t01_counter')
m
<module 'scikitplot_cython_6cd188b74ff0b93f' from '/home/circleci/.cache/scikitplot/cython/6cd188b74ff0b93f5c97775f002cd0925cb51346fb775a5d1a25f7712f4da0ee/scikitplot_cython_6cd188b74ff0b93f.cpython-311-x86_64-linux-gnu.so'>
[]

Tags: model-type: classification model-workflow: model building plot-type: cython domain: cython level: beginner purpose: showcase

Total running time of the script: (0 minutes 4.809 seconds)

Related examples

visualkeras: Vector Index DB

visualkeras: Vector Index DB

Workflow templates (train / hpo / predict) + CLI entry template

Workflow templates (train / hpo / predict) + CLI entry template

annoy.Index python-api with examples

annoy.Index python-api with examples

annoy.Annoy legacy c-api with examples

annoy.Annoy legacy c-api with examples

Gallery generated by Sphinx-Gallery