.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_plot_6_function_identifier.py>`     to download the full example code or to run this example in your browser via Binder
    .. rst-class:: sphx-glr-example-title

    .. _sphx_glr_auto_examples_plot_6_function_identifier.py:


Identifying function names in a script
======================================

This demonstrates how Sphinx-Gallery identifies function names to figure out
which functions are called in the script and to which module do they belong.


.. code-block:: python3


    # Code source: Óscar Nájera
    # License: BSD 3 clause

    import os  # noqa, analysis:ignore
    import matplotlib.pyplot as plt
    from sphinx_gallery.backreferences import identify_names
    from sphinx_gallery.py_source_parser import split_code_and_text_blocks

    filename = os.__file__.replace('.pyc', '.py')
    _, script_blocks = split_code_and_text_blocks(filename)
    names = identify_names(script_blocks)
    figheight = len(names) + .5

    fontsize = 12.5








Sphinx-Gallery examines both the executed code itself, as well as the
documentation blocks (such as this one, or the top-level one),
to find backreferences. This means that by writing :obj:`numpy.sin`
and :obj:`numpy.exp` here, a backreference will be created even though
they are not explicitly used in the code. This is useful in particular when
functions return classes -- if you add them to the documented blocks of
examples that use them, they will be shown in the backreferences.

Also note that global variables of the script have intersphinx references
added to them automatically (e.g., ``fig`` and ``fig.text`` below).


.. code-block:: python3


    fig = plt.figure(figsize=(7.5, 8))

    for i, (name, obj) in enumerate(names.items()):
        fig.text(0.55, (float(len(names)) - 0.5 - i) / figheight,
                 name,
                 ha="right",
                 size=fontsize,
                 transform=fig.transFigure,
                 bbox=dict(boxstyle='square', fc="w", ec="k"))
        fig.text(0.6, (float(len(names)) - 0.5 - i) / figheight,
                 obj[0]["module"],
                 ha="left",
                 size=fontsize,
                 transform=fig.transFigure,
                 bbox=dict(boxstyle='larrow,pad=0.1', fc="w", ec="k"))

    plt.draw()



.. image:: /auto_examples/images/sphx_glr_plot_6_function_identifier_001.png
    :alt: plot 6 function identifier
    :class: sphx-glr-single-img





**Estimated memory usage:**  9 MB


.. _sphx_glr_download_auto_examples_plot_6_function_identifier.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example


  .. container:: binder-badge

    .. image:: images/binder_badge_logo.svg
      :target: https://mybinder.org/v2/gh/sphinx-gallery/sphinx-gallery.github.io/master?urlpath=lab/tree/notebooks/auto_examples/plot_6_function_identifier.ipynb
      :alt: Launch binder
      :width: 150 px


  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_6_function_identifier.py <plot_6_function_identifier.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_6_function_identifier.ipynb <plot_6_function_identifier.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
