def test_identify_names2(tmpdir): """Test more name identification.""" code_str = b""" ''' Title ----- This is an example. ''' # -*- coding: utf-8 -*- # \xc3\x9f from a.b import c import d as e print(c) e.HelloWorld().f.g """ expected = { 'c': { 'name': 'c', 'module': 'a.b', 'module_short': 'a.b', 'is_class': False, }, 'e.HelloWorld': { 'name': 'HelloWorld', 'module': 'd', 'module_short': 'd', 'is_class': False, } } fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res code_str = b""" ''' Title ----- This example uses :func:`h.i`. ''' """ + code_str.split(b"'''")[-1] expected['h.i'] = { u'module': u'h', u'module_short': u'h', u'name': u'i', 'is_class': False } fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res
def test_identify_names2(tmpdir): code_str = b""" ''' Title ----- This is an example. ''' # -*- coding: utf-8 -*- # \xc3\x9f from a.b import c import d as e print(c) e.HelloWorld().f.g """ expected = { 'c': { 'name': 'c', 'module': 'a.b', 'module_short': 'a.b' }, 'e.HelloWorld': { 'name': 'HelloWorld', 'module': 'd', 'module_short': 'd' } } fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') res = sg.identify_names(fname.strpath) assert expected == res code_str = b""" ''' Title ----- This example uses :func:`h.i`. ''' """ + code_str.split(b"'''")[-1] expected['h.i'] = {u'module': u'h', u'module_short': u'h', u'name': u'i'} fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') res = sg.identify_names(fname.strpath) assert expected == res
def test_identify_names(unicode_sample): """Test name identification.""" expected = { 'os.path.join': { 'name': 'join', 'module': 'os.path', 'module_short': 'os.path', 'is_class': False, }, 'br.identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references', 'is_class': False, }, 'identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references', 'is_class': False, }, } _, script_blocks = split_code_and_text_blocks(unicode_sample) res = sg.identify_names(script_blocks) assert expected == res
def test_identify_names2(tmpdir): code_str = b""" # -*- coding: utf-8 -*- # \xc3\x9f from a.b import c import d as e print(c) e.HelloWorld().f.g """ expected = { 'c': { 'name': 'c', 'module': 'a.b', 'module_short': 'a.b' }, 'e.HelloWorld': { 'name': 'HelloWorld', 'module': 'd', 'module_short': 'd' } } fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') res = sg.identify_names(fname.strpath) assert expected == res
def test_identify_names(): code_str = """ import os os os.path.join import sphinx_gallery.back_references as br br.identify_names from sphinx_gallery.back_references import identify_names identify_names """ res = sg.identify_names(code_str) expected = { 'os.path.join': { 'name': 'join', 'module': 'os.path', 'module_short': 'os.path' }, 'br.identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references' }, 'identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references' } } assert expected == res
def test_identify_names2(tmpdir): code_str = b""" ''' Title ----- This is an example. ''' # -*- coding: utf-8 -*- # \xc3\x9f from a.b import c import d as e print(c) e.HelloWorld().f.g """ expected = {'c': {'name': 'c', 'module': 'a.b', 'module_short': 'a.b'}, 'e.HelloWorld': {'name': 'HelloWorld', 'module': 'd', 'module_short': 'd'}} fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') res = sg.identify_names(fname.strpath) assert expected == res code_str = b""" ''' Title ----- This example uses :func:`h.i`. ''' """ + code_str.split(b"'''")[-1] expected['h.i'] = {u'module': u'h', u'module_short': u'h', u'name': u'i'} fname = tmpdir.join("indentify_names.py") fname.write(code_str, 'wb') res = sg.identify_names(fname.strpath) assert expected == res
def test_identify_names(unicode_sample): expected = { 'os.path.join': {'name': 'join', 'module': 'os.path', 'module_short': 'os.path'}, 'br.identify_names': {'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references'}, 'identify_names': {'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references'} } res = sg.identify_names(unicode_sample) assert expected == res
def test_identify_names(unicode_sample): """Test name identification.""" expected = { 'os.path.join': { 'name': 'join', 'module': 'os.path', 'module_short': 'os.path' }, 'br.identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references' }, 'identify_names': { 'name': 'identify_names', 'module': 'sphinx_gallery.back_references', 'module_short': 'sphinx_gallery.back_references' } } res = sg.identify_names(unicode_sample) assert expected == res
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 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).
def test_identify_names2(tmpdir): """Test more name identification.""" code_str = b""" ''' Title ----- This is an example. ''' # -*- coding: utf-8 -*- # \xc3\x9f from a.b import c import d as e import h.i print(c) e.HelloWorld().f.g h.i.j() """ expected = { 'c': [{ 'name': 'c', 'module': 'a.b', 'module_short': 'a.b', 'is_class': False, }], 'e.HelloWorld': [{ 'name': 'HelloWorld', 'module': 'd', 'module_short': 'd', 'is_class': False, }], 'h.i.j': [{ 'name': 'j', 'module': 'h.i', 'module_short': 'h.i', 'is_class': False, }], } fname = tmpdir.join("identify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res code_str = b""" ''' Title ----- This example uses :func:`k.l` and :meth:`~m.n`. ''' """ + code_str.split(b"'''")[-1] expected['k.l'] = [{ u'module': u'k', u'module_short': u'k', u'name': u'l', 'is_class': False }] expected['m.n'] = [{ u'module': u'm', u'module_short': u'm', u'name': u'n', 'is_class': False }] fname = tmpdir.join("identify_names.py") fname.write(code_str, 'wb') _, script_blocks = split_code_and_text_blocks(fname.strpath) res = sg.identify_names(script_blocks) assert expected == res
Identifying function names in a script ====================================== Calls Sphinx-Gallery identify names function to figure out which functions are called in the script and to which module do they belong. """ # Code source: Óscar Nájera # License: BSD 3 clause import matplotlib.pyplot as plt import sphinx_gallery.backreferences as spback filename = spback.__file__.replace('.pyc', '.py') file_code = open(filename, 'r').read() names = spback.identify_names(file_code) figheight = len(names) + .5 fontsize = 22 fig = plt.figure() for i, (name, obj) in enumerate(names.items()): fig.text(0.3, (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.35, (float(len(names)) - 0.5 - i) / figheight, obj["module"], ha="left",
""" Identifying function names in a script ====================================== Calls Sphinx-Gallery identify names function to figure out which functions are called in the script and to which module do they belong. """ # Code source: Óscar Nájera # License: BSD 3 clause import matplotlib.pyplot as plt import sphinx_gallery.backreferences as spback filename = spback.__file__.replace('.pyc', '.py') names = spback.identify_names(filename) figheight = len(names) + .5 fontsize = 22 fig = plt.figure() for i, (name, obj) in enumerate(names.items()): fig.text(0.3, (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.35, (float(len(names)) - 0.5 - i) / figheight, obj["module"], ha="left",
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 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 filename = os.__file__.replace('.pyc', '.py') names = identify_names(filename) figheight = len(names) + .5 fontsize = 20 ############################################################################### # 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. fig = plt.figure(figsize=(7.5, 8))
====================================== Calls Sphinx-Gallery identify names function to figure out which functions are called in the script and to which module do they belong. """ # Code source: Óscar Nájera # License: BSD 3 clause import matplotlib.pyplot as plt import sphinx_gallery.backreferences as spback filename = spback.__file__.replace('.pyc', '.py') file_code = open(filename, 'r').read() names = spback.identify_names(file_code) figheight = len(names)+.5 fontsize = 22 fig = plt.figure() for i, (name, obj) in enumerate(names.items()): fig.text(0.3, (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.35, (float(len(names)) - 0.5 - i)/figheight, obj["module"], ha="left",
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 source: Óscar Nájera # License: BSD 3 clause import os # noqa, analysis:ignore import matplotlib.pyplot as plt import sphinx_gallery.backreferences as spback filename = spback.__file__.replace('.pyc', '.py') names = spback.identify_names(filename) figheight = len(names) + .5 fontsize = 20 ############################################################################### # 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. fig = plt.figure(figsize=(7.5, 8))