.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cython/plot_03_cache_and_restart_reuse.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_03_cache_and_restart_reuse.py: Cache and restart reuse ======================= .. currentmodule:: scikitplot.cython Compiled artifacts are cached on disk. After a Python kernel restart, you can import the cached module again without recompiling (as long as the runtime fingerprint matches). This example demonstrates: - compiling a snippet - listing cached entries - importing the same module from the cache by key - (optional) showing cache stats .. GENERATED FROM PYTHON SOURCE LINES 18-22 .. code-block:: Python # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: Python from __future__ import annotations from typing import Any from scikitplot import cython .. GENERATED FROM PYTHON SOURCE LINES 30-49 .. code-block:: Python def _print_entry_summary(entries: list[Any], limit: int = 5) -> None: """Print a stable cache listing summary.""" print("Cache entries:", len(entries)) if not entries: return # Print last few entries by whatever order list_cached returns. tail = entries[-limit:] print(f"Last {len(tail)} entries:") for e in tail: # CacheEntry is expected to have these (defaults exist). print(" - key:", getattr(e, "key", None)) print(" module_name:", getattr(e, "module_name", None)) print(" artifact_path:", getattr(e, "artifact_path", None)) print(" created_utc:", getattr(e, "created_utc", None)) .. GENERATED FROM PYTHON SOURCE LINES 50-52 Generate `python` Module from `python` -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 52-99 .. code-block:: Python report = cython.check_build_prereqs(numpy=False) if not report.get('cython', {}).get('ok'): print("Skipping compilation because build prerequisites are missing.") problems = report.get("problems", []) if problems: print("Problems:", problems) else: # 1) Compile a snippet and capture its deterministic cache key. r = cython.compile_and_load_result("def f(int n):\n return n*n\n", profile="fast-debug", verbose=0) key = r.key print("\nBuildResult:") print(" module_name:", r.module_name) print(" key :", r.key) print(" artifact :", r.artifact_path) print(" used_cache :", r.used_cache) # 2) List cache entries (safe if empty). entries = cython.list_cached() _print_entry_summary(entries) # Optional: cache stats snapshot (if available). if hasattr(cython, "cache_stats"): print("\nCache stats snapshot:") print(cython.cache_stats()) # 3) Import the same module by key (restart-safe mechanism). # Use import_cached_result if available so we can inspect metadata. if hasattr(cython, "import_cached_result"): r2 = cython.import_cached_result(key) m2 = r2.module print("\nImported from cache (result API):") print(" module_name:", r2.module_name) print(" used_cache :", r2.used_cache) print(" artifact :", r2.artifact_path) else: m2 = cython.import_cached(key) print("\nImported from cache (module API):", m2.__name__) # 4) Verify correctness strictly. out = m2.f(11) print("import_cached(key).f(11) =", out) print("Expected:", 121) print("Correct:", out == 121) .. rst-class:: sphx-glr-script-out .. code-block:: none BuildResult: module_name: scikitplot_cython_c71820fcbdd3d3be key : c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d artifact : /home/circleci/.cache/scikitplot/cython/c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d/scikitplot_cython_c71820fcbdd3d3be.cpython-311-x86_64-linux-gnu.so used_cache : True Cache entries: 5 Last 5 entries: - key: 093729e666d9308a2ee01f667576da3e7022aaf802855405f2fe6727629b9a2a module_name: scikitplot_cython_093729e666d9308a artifact_path: /home/circleci/.cache/scikitplot/cython/093729e666d9308a2ee01f667576da3e7022aaf802855405f2fe6727629b9a2a/scikitplot_cython_093729e666d9308a.cpython-311-x86_64-linux-gnu.so created_utc: 2026-02-01T06:33:52Z - key: 106892295832cfc7f01dbb65bd06b364352040aef37075f2634c508019b279d1 module_name: scikitplot_cython_106892295832cfc7 artifact_path: /home/circleci/.cache/scikitplot/cython/106892295832cfc7f01dbb65bd06b364352040aef37075f2634c508019b279d1/scikitplot_cython_106892295832cfc7.cpython-311-x86_64-linux-gnu.so created_utc: 2026-02-01T06:33:53Z - key: 632e06e9a1a7a87769ae8f9e448b50fa2865f5211a9c7fac72db477b95832e8d module_name: scikitplot_cython_632e06e9a1a7a877 artifact_path: /home/circleci/.cache/scikitplot/cython/632e06e9a1a7a87769ae8f9e448b50fa2865f5211a9c7fac72db477b95832e8d/scikitplot_cython_632e06e9a1a7a877.cpython-311-x86_64-linux-gnu.so created_utc: 2026-02-01T06:33:52Z - key: b89f3272eca6106c5e5079f8fd5b949ad4898c3afd6de5904de4096323a5e9fb module_name: scikitplot_cython_b89f3272eca6106c artifact_path: /home/circleci/.cache/scikitplot/cython/b89f3272eca6106c5e5079f8fd5b949ad4898c3afd6de5904de4096323a5e9fb/scikitplot_cython_b89f3272eca6106c.cpython-311-x86_64-linux-gnu.so created_utc: 2026-02-01T06:33:54Z - key: c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d module_name: scikitplot_cython_c71820fcbdd3d3be artifact_path: /home/circleci/.cache/scikitplot/cython/c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d/scikitplot_cython_c71820fcbdd3d3be.cpython-311-x86_64-linux-gnu.so created_utc: 2026-02-01T06:33:52Z Cache stats snapshot: CacheStats(cache_root=PosixPath('/home/circleci/.cache/scikitplot/cython'), n_modules=6, n_packages=0, total_bytes=3757231, pinned_aliases=1, pinned_keys=1, newest_mtime_utc='2026-02-01T06:33:54Z', oldest_mtime_utc='2026-02-01T06:33:52Z') Imported from cache (result API): module_name: scikitplot_cython_c71820fcbdd3d3be used_cache : True artifact : /home/circleci/.cache/scikitplot/cython/c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d/scikitplot_cython_c71820fcbdd3d3be.cpython-311-x86_64-linux-gnu.so import_cached(key).f(11) = 121 Expected: 121 Correct: True .. GENERATED FROM PYTHON SOURCE LINES 100-105 .. tags:: domain: cython plot-type: cython purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.009 seconds) .. _sphx_glr_download_auto_examples_cython_plot_03_cache_and_restart_reuse.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_03_cache_and_restart_reuse.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_03_cache_and_restart_reuse.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_cache_and_restart_reuse.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_cache_and_restart_reuse.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_cache_and_restart_reuse.zip ` .. include:: plot_03_cache_and_restart_reuse.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_