1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
.. _examples:
Examples
========
.. toctree::
:hidden:
Demo Module <demo_module>
Demo Class <demo_class>
Demo Exception <demo_exception>
Demo Grouper <demo_grouper>
Including a table of contents
-----------------------------
Consider for example the following module in ``dummy.py``:
.. literalinclude:: dummy.py
The *autosummary* flag introduces a small table of contents. So::
.. automodule:: dummy
:members:
:autosummary:
produces :ref:`this <demo_module>`. And::
.. autoclass:: dummy.MyClass
:members:
:autosummary:
produces :ref:`this <demo_class>`, and for exceptions::
.. autoexception:: dummy.MyException
:members:
:autosummary:
produces :ref:`this <demo_exception>`.
By default, module members are (mainly) grouped according into *Functions*,
*Classes* and *Data*, class members are grouped into *Methods* and
*Attributes*. But you can also provide alternative section names by connecting
to the :event:`autodocsumm-grouper` event. For example, if you include::
def example_grouper(app, what, name, obj, section, options, parent):
import dummy
if parent is dummy.MyClass and name == 'some_other_attr':
return 'Alternative Section'
def setup(app):
app.connect('autodocsumm-grouper', example_grouper)
in your *conf.py*, you get :ref:`this <demo_grouper>`.
Note that you can also include the *autosummary* flag in the
:confval:`autodoc_default_options` configuration value
Including the ``__call__`` method
---------------------------------
Suppose you have a descriptor with a ``__call__`` method (i.e. somewhat like
a method with additional features).
.. literalinclude:: call_demo.py
Then, if you set ``autodata_content = 'both'`` in your *conf.py* you get via::
.. autoclass:: call_demo.MyClass
:noindex:
:members:
:undoc-members:
.. autoclass:: call_demo.MyClass
:noindex:
:members:
:undoc-members:
Skip large data representations
-------------------------------
You can exclude large data representations via the :confval:`not_document_data`
and :confval:`document_data` configuration values.
Suppose you have a dictionary with a very large representation, e.g. in the
file `no_data_demo.py`
.. literalinclude:: no_data_demo.py
which you convert to
.. autodata:: keep_data_demo.d
:noindex:
You can skip this if you specify ``not_document_data = ['no_data_demo.d']`` in
your *conf.py*. Then you get
.. autodata:: no_data_demo.d
:noindex:
.. _summary-table-example:
Generating a summary table without the full documentation
---------------------------------------------------------
Using one of the ``autosummary-...`` options (e.g. ``autosummary-members``,
see :ref:`autodoc-flags`) let's you create a summary table that points to the
documentation in another point of the documentation. You should, however make
sure to add the ``noindex`` flag and to add a ``no-members`` flag. For our
:mod:`autodocsumm` module this for example then looks like::
.. automodule:: autodocsumm
:noindex:
:no-members:
:autosummary:
:autosummary-members:
which gives us
.. automodule:: autodocsumm
:noindex:
:no-members:
:autosummary:
:autosummary-members:
.. _no-nesting-example:
Generating a summary table for the module without nesting
---------------------------------------------------------
Using the ``autosummary-no-nesting`` option, you can generate the autosummary
table for a module without generating autosummary tables for members within
that module. This is useful when you only want to use the autosummary table as
a table of contents for a given page. For the :doc:`demo module <demo_module>`,
here's an example::
.. automodule:: dummy
:autosummary:
:members:
:autosummary-no-nesting:
which gives us
.. automodule:: dummy
:noindex:
:members:
:autosummary:
:autosummary-no-nesting:
.. _nosignatures-example:
Generating a summary table without signatures
---------------------------------------------
Using the ``autosummary-nosignatures`` option, you can generate the
autosummary table for a module that will not include function signatures.
This is useful for giving a high-level overview of the function name and
description. For the :doc:`demo module <demo_module>`, here's an example::
.. automodule:: dummy
:members:
:autosummary:
:autosummary-nosignatures:
which gives us
.. automodule:: dummy
:members:
:autosummary:
:autosummary-nosignatures:
.. _select-sections-example:
Select the sections to document
-------------------------------
You can select, which sections to document. If you only want the *Methods*
section of a class, you can specify::
.. autoclass:: dummy.MyClass
:members:
:autosummary:
:autosummary-sections: Methods
Multiple sections might be separated by `;;`, e.g.
``:autosummary-sections: Methods ;; Attributes``.
This also works for the ``autoclasssumm``, ``autoexceptionsumm`` and
``automodulesumm`` directives, e.g.::
.. autoclasssumm:: dummy.SomeClass
:autosummary-sections: Methods
.. autoclasssumm:: dummy.MyClass
:autosummary-sections: Methods
.. _autosummary-position-example:
Positioning of the autosummary table
------------------------------------
By default, the autosummary tables are put at the end of the docstring sections
for classes and methods, i.e. something like::
class-name
class-docstring
autosummary-tables
class-members
You can customize this positioning by calling the ``.. autoclasssumm::``
directive inside the class docstring, e.g.
.. literalinclude:: inline_autoclasssumm.py
Documenting this, with ``.. autoclass:: MyClass`` creates
.. autoclass:: inline_autoclasssumm.MyClass
:noindex:
:members:
Note that this omits the `Attributes` section as we specified the
``:autosummary-sections:`` options here. If you want to list this section
anyway at the end of the docstring, you can use the ``autosummary-force-inline``
option, e.g.::
.. autoclass:: inline_autoclasssumm.MyClass
:members:
:autosummary-force-inline:
:autosummary-sections: Attributes
.. autoclass:: inline_autoclasssumm.MyClass
:members:
:noindex:
:autosummary-force-inline:
:autosummary-sections: Attributes
|