Skip to content

Commit a99a3c3

Browse files
authored
Add Python syntax highlighting to README.rst (#395)
Verified exhaustiveness with: ```bash find -name '*.rst' ```
2 parents 3ae050f + 864e9f4 commit a99a3c3

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

README.rst

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ then run the script with a special script (in this case with specific
5858
arguments to the Python interpreter).
5959

6060
In the following example, we create a simple function ``my_func`` that
61-
allocates lists ``a``, ``b`` and then deletes ``b``::
61+
allocates lists ``a``, ``b`` and then deletes ``b``:
6262

63+
.. code-block:: python
6364
6465
@profile
6566
def my_func():
@@ -101,7 +102,9 @@ the number of times that profiler has executed each line. The last column
101102

102103
Decorator
103104
=========
104-
A function decorator is also available. Use as follows::
105+
A function decorator is also available. Use as follows:
106+
107+
.. code-block:: python
105108
106109
from memory_profiler import profile
107110
@@ -116,7 +119,9 @@ In this case the script can be run without specifying ``-m
116119
memory_profiler`` in the command line.
117120

118121
In function decorator, you can specify the precision as an argument to the
119-
decorator function. Use as follows::
122+
decorator function. Use as follows:
123+
124+
.. code-block:: python
120125
121126
from memory_profiler import profile
122127
@@ -282,6 +287,7 @@ necessarily a Python program), a string containing some python code to
282287
be evaluated or a tuple ``(f, args, kw)`` containing a function and its
283288
arguments to be evaluated as ``f(*args, **kw)``. For example,
284289

290+
.. code-block:: python
285291
286292
>>> from memory_profiler import memory_usage
287293
>>> mem_usage = memory_usage(-1, interval=.2, timeout=1)
@@ -301,8 +307,9 @@ thing on the IPython notebook it scales up to 44MB.
301307

302308
If you'd like to get the memory consumption of a Python function, then
303309
you should specify the function and its arguments in the tuple ``(f,
304-
args, kw)``. For example::
310+
args, kw)``. For example:
305311

312+
.. code-block:: python
306313
307314
>>> # define a simple function
308315
>>> def f(a, n=100):
@@ -325,6 +332,8 @@ REPORTING
325332
The output can be redirected to a log file by passing IO stream as
326333
parameter to the decorator like @profile(stream=fp)
327334

335+
.. code-block:: python
336+
328337
>>> fp=open('memory_profiler.log','w+')
329338
>>> @profile(stream=fp)
330339
>>> def my_func():
@@ -333,7 +342,7 @@ parameter to the decorator like @profile(stream=fp)
333342
... del b
334343
... return a
335344
336-
For details refer: examples/reporting_file.py
345+
For details refer: examples/reporting_file.py
337346

338347
``Reporting via logger Module:``
339348

@@ -343,6 +352,8 @@ when we need to use RotatingFileHandler.
343352
The output can be redirected to logger module by simply making use of
344353
LogFile of memory profiler module.
345354

355+
.. code-block:: python
356+
346357
>>> from memory_profiler import LogFile
347358
>>> import sys
348359
>>> sys.stdout = LogFile('memory_profile_log')
@@ -354,11 +365,13 @@ could be cumbersome and one can choose only entries with increments
354365
by passing True to reportIncrementFlag, where reportIncrementFlag is
355366
a parameter to LogFile class of memory profiler module.
356367

368+
.. code-block:: python
369+
357370
>>> from memory_profiler import LogFile
358371
>>> import sys
359372
>>> sys.stdout = LogFile('memory_profile_log', reportIncrementFlag=False)
360373
361-
For details refer: examples/reporting_logger.py
374+
For details refer: examples/reporting_logger.py
362375

363376
=====================
364377
IPython integration
@@ -372,7 +385,9 @@ For IPython 0.11+, you can use the module directly as an extension, with
372385
To activate it whenever you start IPython, edit the configuration file for your
373386
IPython profile, ~/.ipython/profile_default/ipython_config.py, to register the
374387
extension like this (If you already have other extensions, just add this one to
375-
the list)::
388+
the list):
389+
390+
.. code-block:: python
376391
377392
c.InteractiveShellApp.extensions = [
378393
'memory_profiler',
@@ -385,28 +400,36 @@ It then can be used directly from IPython to obtain a line-by-line
385400
report using the `%mprun` or `%%mprun` magic command. In this case, you can skip
386401
the `@profile` decorator and instead use the `-f` parameter, like
387402
this. Note however that function my_func must be defined in a file
388-
(cannot have been defined interactively in the Python interpreter)::
403+
(cannot have been defined interactively in the Python interpreter):
404+
405+
.. code-block:: python
389406
390407
In [1]: from example import my_func, my_func_2
391408
392409
In [2]: %mprun -f my_func my_func()
393410
394-
or in cell mode::
411+
or in cell mode:
412+
413+
.. code-block:: python
395414
396415
In [3]: %%mprun -f my_func -f my_func_2
397416
...: my_func()
398417
...: my_func_2()
399418
400419
Another useful magic that we define is `%memit`, which is analogous to
401-
`%timeit`. It can be used as follows::
420+
`%timeit`. It can be used as follows:
421+
422+
.. code-block:: python
402423
403424
In [1]: %memit range(10000)
404425
peak memory: 21.42 MiB, increment: 0.41 MiB
405426
406427
In [2]: %memit range(1000000)
407428
peak memory: 52.10 MiB, increment: 31.08 MiB
408429
409-
or in cell mode (with setup code)::
430+
or in cell mode (with setup code):
431+
432+
.. code-block:: python
410433
411434
In [3]: %%memit l=range(1000000)
412435
...: len(l)
@@ -416,7 +439,9 @@ or in cell mode (with setup code)::
416439
For more details, see the docstrings of the magics.
417440

418441
For IPython 0.10, you can install it by editing the IPython configuration
419-
file ~/.ipython/ipy_user_conf.py to add the following lines::
442+
file ~/.ipython/ipy_user_conf.py to add the following lines:
443+
444+
.. code-block:: python
420445
421446
# These two lines are standard and probably already there.
422447
import IPython.ipapi
@@ -441,6 +466,8 @@ Currently, the backend can be set via the CLI
441466

442467
and is exposed by the API
443468

469+
.. code-block:: python
470+
444471
>>> from memory_profiler import memory_usage
445472
>>> mem_usage = memory_usage(-1, interval=.2, timeout=1, backend="psutil")
446473

0 commit comments

Comments
 (0)