
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/plot_6_function_identifier.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

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

        :ref:`Go to the end <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 when

1. a function/method/attribute/object is used or class instantiated in a
   code block
2. a function/method/attribute/object/class is referred to using sphinx markup
   in a text block.

Sphinx-Gallery examines both the executed code itself, as well as the
text blocks (such as this one, or the one below) for these references and
identifies the module they belong to. This means that by writing
:obj:`numpy.sin` and :obj:`numpy.exp` here, they will be identified even though
they are not used in the code. This is useful in particular when functions
return classes (meaning it is not explicitly instantiated) -- if you add them
to the documented blocks of examples that use them, they will be added to
backreferences.

This functionality is used to add documentation hyperlinks to your code
(:ref:`link_to_documentation`) and for mini-gallery creation
(:ref:`references_to_examples`).

.. GENERATED FROM PYTHON SOURCE LINES 25-50

.. code-block:: Python


    # Code source: Óscar Nájera
    # License: BSD 3 clause
    # sphinx_gallery_tags = ["matplotlib","demo"]

    import os.path as op  # noqa, analysis:ignore

    import matplotlib.pyplot as plt

    import sphinx_gallery
    from sphinx_gallery.backreferences import _make_ref_regex, identify_names
    from sphinx_gallery.py_source_parser import split_code_and_text_blocks

    filename = "plot_6_function_identifier.py"
    if not op.exists(filename):
        filename = (
            __file__
            if "__file__" in locals()
            else op.join(op.dirname(sphinx_gallery.__path__[0]), "examples", filename)
        )

    _, script_blocks = split_code_and_text_blocks(filename)

    names = identify_names(script_blocks, _make_ref_regex())








.. GENERATED FROM PYTHON SOURCE LINES 51-55

In the code block above, we use the internal function ``identify_names`` to
obtain all identified names from this file and their full resolved import
path. We then plot this below, where the identified names functions are
on the left and the full resolved import path is on the right.

.. GENERATED FROM PYTHON SOURCE LINES 55-84

.. code-block:: Python


    fontsize = 12.5
    figheight = 3 * len(names) * fontsize / 72
    fig, ax = plt.subplots(figsize=(7.5, figheight))
    ax.set_visible(False)

    for i, (name, obj) in enumerate(names.items(), 1):
        fig.text(
            0.48,
            1 - i / (len(names) + 1),
            name,
            ha="right",
            va="center",
            size=fontsize,
            transform=fig.transFigure,
            bbox=dict(boxstyle="square", fc="w", ec="k"),
        )
        fig.text(
            0.52,
            1 - i / (len(names) + 1),
            obj[0]["module"],
            ha="left",
            va="center",
            size=fontsize,
            transform=fig.transFigure,
            bbox=dict(boxstyle="larrow,pad=0.1", fc="w", ec="k"),
        )

    plt.show()



.. image-sg:: /auto_examples/images/sphx_glr_plot_6_function_identifier_001.png
   :alt: plot 6 function identifier
   :srcset: /auto_examples/images/sphx_glr_plot_6_function_identifier_001.png, /auto_examples/images/sphx_glr_plot_6_function_identifier_001_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-example-tags

   🏷 Tags: matplotlib, demo


.. _sphx_glr_download_auto_examples_plot_6_function_identifier.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/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-jupyter

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

    .. 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-zip

      :download:`Download zipped: plot_6_function_identifier.zip <plot_6_function_identifier.zip>`


.. only:: html

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

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