.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cython/plot_cython_template.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via JupyterLite or Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_cython_plot_cython_template.py: Cython: Realtime compile_and_load (.pyx) ====================================================================== .. currentmodule:: scikitplot.cython An example showing the :py:mod:`~scikitplot.cython` submodule.. .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 15-17 cython examples --------------- .. GENERATED FROM PYTHON SOURCE LINES 17-21 .. code-block:: Python import scikitplot as sp sp.__version_iso_8601__ .. rst-class:: sphx-glr-script-out .. code-block:: none '2026.02.01' .. GENERATED FROM PYTHON SOURCE LINES 22-27 .. code-block:: Python from scikitplot import cython print(cython.__doc__) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: Python from scikitplot.cython import compile_and_load m = compile_and_load("def f(int n):\n return n*n") m.f(10) .. rst-class:: sphx-glr-script-out .. code-block:: none 100 .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python cython.list_templates() .. rst-class:: sphx-glr-script-out .. code-block:: none ['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'] .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python cython.get_template_path(cython.list_templates()[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none PosixPath('/home/circleci/.pyenv/versions/3.11.14/lib/python3.11/site-packages/scikitplot/cython/_templates/basic_cython/t01_square_int.pyx') .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: Python print(cython.read_template(cython.list_templates()[0])) .. rst-class:: sphx-glr-script-out .. code-block:: none # 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 .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python cython.get_cache_dir() .. rst-class:: sphx-glr-script-out .. code-block:: none PosixPath('/home/circleci/.cache/scikitplot/cython') .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: Python cython.list_cached() .. rst-class:: sphx-glr-script-out .. code-block:: none [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'})] .. GENERATED FROM PYTHON SOURCE LINES 54-57 .. code-block:: Python cython.list_cached()[0].build_dir .. rst-class:: sphx-glr-script-out .. code-block:: none PosixPath('/home/circleci/.cache/scikitplot/cython/0168cdebb518cec1012769ec306530b313481ce4103cab7a696eedf8e56011c7') .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: Python cython.list_cached()[0].artifact_path .. rst-class:: sphx-glr-script-out .. code-block:: none PosixPath('/home/circleci/.cache/scikitplot/cython/0168cdebb518cec1012769ec306530b313481ce4103cab7a696eedf8e56011c7/scikitplot_cython_0168cdebb518cec1.cpython-311-x86_64-linux-gnu.so') .. GENERATED FROM PYTHON SOURCE LINES 62-70 .. code-block:: Python from scikitplot.cython import compile_template m = compile_template(cython.list_templates()[2]) m = compile_template(cython.list_templates()[1]) m = compile_template(cython.list_templates()[0]) m.square(12) .. rst-class:: sphx-glr-script-out .. code-block:: none 144 .. GENERATED FROM PYTHON SOURCE LINES 71-75 .. code-block:: Python m = compile_template('module_cython/t01_counter') m .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 76-79 .. code-block:: Python cython.purge_cache() .. GENERATED FROM PYTHON SOURCE LINES 80-84 .. code-block:: Python cython.list_cached() .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 85-93 .. tags:: model-type: classification model-workflow: model building plot-type: cython domain: cython level: beginner purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.809 seconds) .. _sphx_glr_download_auto_examples_cython_plot_cython_template.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/scikit-plots/scikit-plots/main?urlpath=lab/tree/notebooks/auto_examples/cython/plot_cython_template.ipynb :alt: Launch binder :width: 150 px .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../lite/lab/index.html?path=auto_examples/cython/plot_cython_template.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_cython_template.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_cython_template.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_cython_template.zip ` .. include:: plot_cython_template.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_