Pull the Python out of one block's body: keep only prompt lines, strip the ``In [n]:``/``...:`` prefix (preserving the code's own indentation), and drop ``@verbatim`` sub-blocks.
(body)
| 68 | |
| 69 | |
| 70 | def _parse_block(body): |
| 71 | """Pull the Python out of one block's body: keep only prompt lines, strip |
| 72 | the ``In [n]:``/``...:`` prefix (preserving the code's own indentation), |
| 73 | and drop ``@verbatim`` sub-blocks.""" |
| 74 | out = [] |
| 75 | skip = False |
| 76 | for ln in body: |
| 77 | s = ln.strip() |
| 78 | if s.startswith("@verbatim"): |
| 79 | skip = True |
| 80 | continue |
| 81 | if s.startswith("@"): # @suppress/@doctest/@savefig markers -> drop the marker |
| 82 | continue |
| 83 | m = _PROMPT.match(ln) |
| 84 | if m is None: |
| 85 | # blank line ends a verbatim run; non-prompt text is output/comment |
| 86 | if s == "": |
| 87 | skip = False |
| 88 | continue |
| 89 | if skip: |
| 90 | continue |
| 91 | code = m.group(1) |
| 92 | # Drop IPython magics / shell escapes (e.g. ``%run``, ``%timeit``, ``!cmd``); |
| 93 | # they are not Python and are not part of the API surface under test. |
| 94 | if code.lstrip().startswith(("%", "!")): |
| 95 | continue |
| 96 | out.append(code) |
| 97 | return "\n".join(out) |
| 98 | |
| 99 | |
| 100 | def _doc_pages(): |
no test coverage detected