.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cython/plot_04_pin_alias.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_04_pin_alias.py: Pin/Alias: stable handles for cached builds =========================================== .. currentmodule:: scikitplot.cython Pins provide a human-friendly alias for a cached build key: - ``pin(key, alias="fast_fft")`` - ``import_pinned("fast_fft")`` Pins are stored per cache directory (portable and deterministic). What this example demonstrates ------------------------------ 1) Compile a snippet and pin its cache key under an alias. 2) Import again using the alias (without remembering the key). 3) Show strict overwrite behavior. 4) List pins and remove a pin (unpin). .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python # Authors: The scikit-plots developers # SPDX-License-Identifier: BSD-3-Clause .. GENERATED FROM PYTHON SOURCE LINES 26-30 .. code-block:: Python from __future__ import annotations from scikitplot import cython .. GENERATED FROM PYTHON SOURCE LINES 31-33 Generate `python` Module from `python` -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-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 small module and capture its 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 alias = "demo_square" print("Built key:", key) print("Pin alias:", alias) # 2) Pin the key (overwrite=True makes the example rerunnable). cython.pin(key, alias=alias, overwrite=True) # 3) Import via alias and verify correctness. if hasattr(cython, "import_pinned_result"): r2 = cython.import_pinned_result(alias) m = r2.module print("\nImported via alias (result API):") print(" module_name:", r2.module_name) print(" key :", r2.key) print(" used_cache :", r2.used_cache) else: m = cython.import_pinned(alias) print("\nImported via alias (module API):", m.__name__) out = m.f(9) print("demo_square.f(9) =", out) print("Expected:", 81) print("Correct:", out == 81) # 4) Show pins dictionary (alias -> key). pins = cython.list_pins() print("\nPins:", pins) print("Alias resolves to this key:", pins.get(alias)) # 5) Demonstrate strict overwrite behavior: # Attempt to pin a different key to the same alias without overwrite. r_other = cython.compile_and_load_result("def f(int n):\n return n*n + 1\n", profile="fast-debug", verbose=0) print("\nStrict collision demo:") try: cython.pin(r_other.key, alias=alias, overwrite=False) print("Unexpected: pin succeeded without overwrite.") except Exception as e: print("As expected, pin without overwrite failed:") print(" ", type(e).__name__ + ":", e) # Now overwrite intentionally. cython.pin(r_other.key, alias=alias, overwrite=True) m_over = cython.import_pinned(alias) print("After overwrite, demo_square.f(9) =", m_over.f(9)) # 6) Unpin and verify alias is removed. if hasattr(cython, "unpin"): cython.unpin(alias) print("\nAfter unpin, pins:", cython.list_pins()) else: print("\nNo unpin() API found in this build; skipping unpin demo.") .. rst-class:: sphx-glr-script-out .. code-block:: none Built key: c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d Pin alias: demo_square Imported via alias (result API): module_name: scikitplot_cython_c71820fcbdd3d3be key : c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d used_cache : True demo_square.f(9) = 81 Expected: 81 Correct: True Pins: {'demo_square': 'c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d', 'quickstart_g': '093729e666d9308a2ee01f667576da3e7022aaf802855405f2fe6727629b9a2a'} Alias resolves to this key: c71820fcbdd3d3be311db100faa9f7ce550548267112a935bc644a9c9e59e29d Strict collision demo: As expected, pin without overwrite failed: ValueError: Alias collision: alias 'demo_square' already points to a different key (c71820fcbdd3d3be...). Use overwrite=True to replace. After overwrite, demo_square.f(9) = 82 After unpin, pins: {'quickstart_g': '093729e666d9308a2ee01f667576da3e7022aaf802855405f2fe6727629b9a2a'} .. 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.471 seconds) .. _sphx_glr_download_auto_examples_cython_plot_04_pin_alias.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_04_pin_alias.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_04_pin_alias.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_pin_alias.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_pin_alias.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_pin_alias.zip ` .. include:: plot_04_pin_alias.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_