.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/cython/plot_07_cpp_mode_basics.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_07_cpp_mode_basics.py: C++ mode basics: cppclass and libcpp containers =============================================== .. currentmodule:: scikitplot.cython This example demonstrates a minimal C++-mode build using: - ``language="c++"`` - ``libcpp.vector`` - a tiny header-only ``cppclass`` shipped via ``support_files`` No external ``.cpp`` sources are required (header-only usage). Notes ----- - ``noexcept`` is intentionally not used in Cython signatures for maximum compatibility. - Some compilers may require an explicit C++ standard flag; see the comment near ``extra_compile_args``. .. 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-31 .. code-block:: Python from __future__ import annotations from scikitplot import cython .. GENERATED FROM PYTHON SOURCE LINES 32-34 Generate `python` Module from `cython` -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 34-125 .. 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.") print(report.get("problems", [])) else: support_files = { "accum.hpp": r""" #pragma once namespace skplt_demo { class Accumulator { public: Accumulator() : total_(0) {} void add(int x) { total_ += x; } int value() const { return total_; } private: int total_; }; } // namespace skplt_demo """ } code = r""" # distutils: language = c++ from libcpp.vector cimport vector cdef extern from "accum.hpp" namespace "skplt_demo": cdef cppclass Accumulator: Accumulator() except + void add(int x) int value() const cdef class PyAccumulator: cdef Accumulator* _p def __cinit__(self): self._p = new Accumulator() def __dealloc__(self): del self._p def add(self, int x): self._p.add(x) def value(self): return self._p.value() def sum_vector(): cdef vector[int] v v.push_back(1) v.push_back(2) v.push_back(3) cdef int s = 0 cdef int x for x in v: s += x return s def accumulate_demo(): a = PyAccumulator() a.add(10) a.add(20) return a.value() """ try: r = cython.compile_and_load_result( code, module_name="cpp_mode_demo", profile="fast-debug", language="c++", support_files=support_files, compiler_directives={"language_level": 3, "embedsignature": True}, # Optional: if you hit "unknown type" or stdlib issues on older toolchains, # set an explicit standard. Keep it user-controlled (no guessing). # extra_compile_args=["-std=c++11"], verbose=0, ) except Exception as e: print("C++ build failed:") print(type(e).__name__ + ":", e) else: print("sum_vector() =", r.module.sum_vector(), "(expected 6)") print("accumulate_demo() =", r.module.accumulate_demo(), "(expected 30)") .. rst-class:: sphx-glr-script-out .. code-block:: none sum_vector() = 6 (expected 6) accumulate_demo() = 30 (expected 30) .. GENERATED FROM PYTHON SOURCE LINES 126-131 .. tags:: domain: cython plot-type: cython purpose: showcase .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.119 seconds) .. _sphx_glr_download_auto_examples_cython_plot_07_cpp_mode_basics.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_07_cpp_mode_basics.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_07_cpp_mode_basics.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_07_cpp_mode_basics.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_07_cpp_mode_basics.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_07_cpp_mode_basics.zip ` .. include:: plot_07_cpp_mode_basics.recommendations .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_