Skip to content

_parser

Warning: The parser is currently an internal concern and changes will not necessarily be reflected in semver.

_parser

Parse notebooks.

CellSource

CellSource(contents: Sequence[str] | str)

Contains source code of a ipynb cell.

  • Initialisable either from a multiline string, or a sequence of strings (one per line)
  • String representation is multiline string
  • Iterates by line

cellmagiclines property

cellmagiclines: set[int]

Return a new CellSource with any lines containing cellmagics commented out.

magiclines property

magiclines: set[int]

Return a list of all lines (starting at 1), the MagicFinder identifies.

muggled cached property

muggled: Self

A version of this Source with magic (and ipytest) lines commented out.

MagicFinder

MagicFinder()

Identifies lines which use ipython magics or call ipytest.

magiclines instance-attribute

magiclines: set[int] = set()

Linenumbers (starting at 1) of lines containing magics/ipytest.

Notebook

Notebook(filepath: Path)

The relevant bits of an ipython Notebook.

ATTRIBUTE DESCRIPTION
muggled_codecells

The code cells excluding any identified as test cells. With magic & ipytest lines commented out.

TYPE: SourceList

muggled_testcells

The code cells which are identified as containing tests, based upon the presence of the %%ipytest magic. With magic & ipytest lines commented out.

TYPE: SourceList

muggled_codecells instance-attribute

muggled_codecells: SourceList = SourceList(
    (
        muggled
        if _iscodecell(cell) and not _istestcell(cell)
        else None
    )
    for cell in cells
)

The code cells excluding any identified as test cells. With magic & ipytest lines commented out.

muggled_testcells instance-attribute

muggled_testcells: SourceList = SourceList(
    muggled if _istestcell(cell) else None for cell in cells
)

The code cells which are identified as containing tests, based upon the presence of the %%ipytestmagic. With magic & ipytest lines commented out.

SourceList

A list[CellSource] with non-continuous indices for storing the contents of cells.

  • use a full slice sourcelist[:], not list(sourcelist) to get contents.
  • supports .ids() analog to a mapping.keys(), yielding only cell-ids with source.

__getitem__

__getitem__(index: SupportsIndex) -> CellSource
__getitem__(index: slice) -> list[CellSource]
__getitem__(index)

Behaves as you would expect for a list with the following exceptions.

  • If provided with a single index: Raises an IndexError if the element at index does not contain any relevant source.
  • If provided with a slice: Returns only those items, which contain relevant source.

ids

ids() -> Generator[int, None, None]

Analog to mapping .keys(), yielding only cell-ids with source.