Skip to content

Commit 06ed818

Browse files
committed
Doc update
docs/source/manual/examples/example_kernprof.rst - Removed assumption that `${PWD}` is in `${PYTHONPATH}` - Replaced `console` code blocks with `bash` code blocks + collapsible output code blocks - Reworked examples in "Literal-code execution": replaced concatenation/`read` into shell variable with multiline strings - Reworked example in "Executing code read from ``stdin``": replaced heredoc with command pipeline
1 parent e0a5e7d commit 06ed818

File tree

1 file changed

+132
-35
lines changed

1 file changed

+132
-35
lines changed

docs/source/manual/examples/example_kernprof.rst

+132-35
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ and profile Python code in various forms.
66

77
For the following, we assume that we have:
88

9-
* the below file ``fib.py`` in the current directory,
10-
* the current directory in ``${PYTHONPATH}``, and
9+
* the below file ``fib.py`` in the current directory, and
1110
* :py:mod:`line_profiler` and :py:mod:`kernprof` installed.
1211

1312
.. code:: python
@@ -62,10 +61,18 @@ Script execution
6261
In the most basic form, one passes the path to the executed script and
6362
its arguments to ``kernprof``:
6463

65-
.. code:: console
64+
.. code:: bash
65+
66+
kernprof --prof-mod fib.py --line-by-line --view \
67+
fib.py --verbose 10 20 30
68+
69+
.. raw:: html
70+
71+
<details>
72+
<summary>Output (click to expand)</summary>
73+
74+
.. code::
6675
67-
$ kernprof --prof-mod fib.py --line-by-line --view \
68-
> fib.py --verbose 10 20 30
6976
fib(10) = 89
7077
fib(20) = 10946
7178
fib(30) = 1346269
@@ -126,6 +133,12 @@ its arguments to ``kernprof``:
126133
37 3 91.0 30.3 18.7 result = func(n)
127134
38 3 20.0 6.7 4.1 print(pattern.format(n, result))
128135
136+
.. raw:: html
137+
138+
</details>
139+
<p>
140+
141+
129142
.. _kernprof-script-note:
130143
.. note::
131144

@@ -141,16 +154,30 @@ Module execution
141154
It is also possible to use ``kernprof -m`` to run installed modules and
142155
packages:
143156

144-
.. code:: console
157+
.. code:: bash
158+
159+
PYTHONPATH="${PYTHONPATH}:${PWD}" \
160+
kernprof --prof-mod fib --line-by-line --view -m \
161+
fib --verbose 10 20 30
162+
163+
.. raw:: html
164+
165+
<details>
166+
<summary>Output (click to expand)</summary>
167+
168+
.. code::
145169
146-
$ kernprof --prof-mod fib --line-by-line --view -m \
147-
> fib --verbose 10 20 30
148170
fib(10) = 89
149171
fib(20) = 10946
150172
fib(30) = 1346269
151173
Wrote profile results to fib.lprof
152174
...
153175
176+
.. raw:: html
177+
178+
</details>
179+
<p>
180+
154181
.. _kernprof-m-note:
155182
.. note::
156183

@@ -159,13 +186,25 @@ packages:
159186
thereafter (the run module).
160187
If there isn't one, an error is raised:
161188

162-
.. code:: console
189+
.. code:: bash
190+
191+
kernprof -m
192+
193+
.. raw:: html
194+
195+
<details>
196+
<summary>Output (click to expand)</summary>
197+
198+
.. code:: pycon
163199
164-
$ kernprof -m
165200
Traceback (most recent call last):
166201
...
167202
ValueError: argument expected for the -m option
168203
204+
.. raw:: html
205+
206+
</details>
207+
169208

170209
Literal-code execution
171210
----------------------
@@ -174,12 +213,23 @@ Like how ``kernprof -m`` parallels ``python -m``, ``kernprof -c`` can be
174213
used to run and profile literal snippets supplied on the command line
175214
like ``python -c``:
176215

177-
.. code:: console
216+
.. code:: bash
217+
218+
PYTHONPATH="${PYTHONPATH}:${PWD}" \
219+
kernprof --prof-mod fib._run_fib --line-by-line --view -c "
220+
import sys
221+
from fib import _run_fib, fib_no_cache as fib
222+
for n in sys.argv[1:]:
223+
print(f'fib({n})', '=', fib(int(n)))
224+
" 10 20
225+
226+
.. raw:: html
227+
228+
<details>
229+
<summary>Output (click to expand)</summary>
230+
231+
.. code::
178232
179-
$ code="import sys; "
180-
$ code+="from fib import _run_fib, fib_no_cache as fib; "
181-
$ code+="for n in sys.argv[1:]: print(f'fib({n})', '=', fib(int(n)))"
182-
$ kernprof --prof-mod fib._run_fib --line-by-line --view -c "${code}" 10 20
183233
fib(10) = 89
184234
fib(20) = 10946
185235
Wrote profile results to <...>/kernprof-command-imuhz89_.lprof
@@ -200,6 +250,11 @@ like ``python -c``:
200250
22 11033 1477.0 0.1 18.4 prev = fib(n - 1)
201251
23 11033 770.0 0.1 9.6 return prev_prev + prev
202252
253+
.. raw:: html
254+
255+
</details>
256+
<p>
257+
203258
.. note::
204259

205260
* As with ``python -c``, the ``-c`` option terminates further
@@ -215,18 +270,25 @@ like ``python -c``:
215270
``python -m line_profiler`` and has to be ``--view``-ed
216271
immediately:
217272

218-
.. code:: console
219-
220-
$ read -d '' -r code <<-'!'
221-
> from fib import fib
222-
>
223-
> def my_func(n=50):
224-
> result = fib(n)
225-
> print(n, '->', result)
226-
>
227-
> my_func()
228-
> !
229-
$ kernprof -lv -c "${code}"
273+
.. code:: bash
274+
275+
PYTHONPATH="${PYTHONPATH}:${PWD}" \
276+
kernprof --line-by-line --view -c "
277+
from fib import fib
278+
279+
def my_func(n=50):
280+
result = fib(n)
281+
print(n, '->', result)
282+
283+
my_func()"
284+
285+
.. raw:: html
286+
287+
<details>
288+
<summary>Output (click to expand)</summary>
289+
290+
.. code::
291+
230292
50 -> 20365011074
231293
Wrote profile results to <...>/kernprof-command-ni6nis6t.lprof
232294
Timer unit: 1e-06 s
@@ -241,7 +303,22 @@ like ``python -c``:
241303
4 1 26.0 26.0 68.4 result = fib(n)
242304
5 1 12.0 12.0 31.6 print(n, '->', result)
243305
244-
$ python -m line_profiler kernprof-command-ni6nis6t.lprof
306+
.. raw:: html
307+
308+
</details>
309+
<p>
310+
311+
.. code:: bash
312+
313+
python -m line_profiler kernprof-command-ni6nis6t.lprof
314+
315+
.. raw:: html
316+
317+
<details>
318+
<summary>Output (click to expand)</summary>
319+
320+
.. code::
321+
245322
Timer unit: 1e-06 s
246323
247324
Total time: 3.6e-05 s
@@ -257,26 +334,46 @@ like ``python -c``:
257334
4 1 26.0 26.0 72.2
258335
5 1 10.0 10.0 27.8
259336
337+
.. raw:: html
338+
339+
</details>
340+
260341

261342
Executing code read from ``stdin``
262343
----------------------------------
263344

264345
It is also possible to read, run, and profile code from ``stdin``, by
265346
passing ``-`` to ``kernprof`` in place of a filename:
266347

267-
.. code:: console
348+
.. code:: bash
349+
350+
{
351+
# This example doesn't make much sense on its own, but just
352+
# imagine if this is a command generating code dynamically
353+
echo 'import sys'
354+
echo 'from fib import _run_fib, fib_no_cache as fib'
355+
echo 'for n in sys.argv[1:]:'
356+
echo ' print(f"fib({n})", "=", fib(int(n)))'
357+
} | PYTHONPATH="${PYTHONPATH}:${PWD}" \
358+
kernprof --prof-mod fib._run_fib --line-by-line --view - 10 20
359+
360+
.. raw:: html
361+
362+
<details>
363+
<summary>Output (click to expand)</summary>
364+
365+
.. code::
268366
269-
$ kernprof --prof-mod fib._run_fib --line-by-line --view - 10 20 <<-'!'
270-
> import sys
271-
> from fib import _run_fib, fib_no_cache as fib
272-
> for n in sys.argv[1:]:
273-
> print(f"fib({n})", "=", fib(int(n)))
274-
> !
275367
fib(10) = 89
276368
fib(20) = 10946
277369
Wrote profile results to <...>/kernprof-stdin-kntk2lo1.lprof
278370
...
279371
372+
.. raw:: html
373+
374+
</details>
375+
<p>
376+
280377
.. note::
281378

282379
Since the temporary file containing the executed code will not exist

0 commit comments

Comments
 (0)