Skip to content

Lookup

mzmlpy.lookup.SpectrumLookup

SpectrumLookup(
    file_object: FileInterface,
    count: int | None = None,
    id_regex: str | None = None,
)

Bases: BaseLookup[Spectrum]

Lookup interface for spectra.

Source code in src/mzmlpy/lookup.py
14
15
16
17
18
def __init__(self, file_object: FileInterface, count: int | None = None, id_regex: str | None = None) -> None:
    self.file_object = file_object
    self._count = count
    self._id_regex = id_regex
    self._cursor: Iterator[T] | None = None

count property

count: int | None

Get count of items.

get_by_index

get_by_index(index: int | str) -> T

Get item by index. Negative indices count from the end, like a list.

Source code in src/mzmlpy/lookup.py
39
40
41
42
43
44
45
46
47
48
def get_by_index(self, index: int | str) -> T:
    """Get item by index. Negative indices count from the end, like a list."""
    if isinstance(index, str):
        index = int(index)
    if index < 0:
        # Normalize against the count so lookup[-1] mirrors slice behavior (lookup[-1:]).
        count = self.count
        if count is not None:
            index += count
    return self._get_by_index_impl(index)

get_by_id

get_by_id(identifier: str) -> T

Get item by ID.

Source code in src/mzmlpy/lookup.py
50
51
52
def get_by_id(self, identifier: str) -> T:
    """Get item by ID."""
    return self._get_by_id_impl(identifier)

get_by_slice

get_by_slice(slice_obj: slice) -> list[T]

Get items by slice notation.

Source code in src/mzmlpy/lookup.py
54
55
56
57
58
59
60
61
62
def get_by_slice(self, slice_obj: slice) -> list[T]:
    """Get items by slice notation."""
    if self.count is None:
        # Don't know count - must iterate all and slice
        items: list[T] = list(self)
        return items[slice_obj]
    # Know count - use slice.indices() to handle all cases
    start, stop, step = slice_obj.indices(self.count)
    return [self.get_by_index(i) for i in range(start, stop, step)]

next

next() -> T

Advance a persistent cursor and return the next item.

The cursor is created on first call and advances on each subsequent call, raising StopIteration once every item has been returned. Call :meth:reset to start over.

Source code in src/mzmlpy/lookup.py
89
90
91
92
93
94
95
96
97
def next(self) -> T:
    """Advance a persistent cursor and return the next item.

    The cursor is created on first call and advances on each subsequent call, raising
    ``StopIteration`` once every item has been returned. Call :meth:`reset` to start over.
    """
    if self._cursor is None:
        self._cursor = iter(self)
    return next(self._cursor)

reset

reset() -> None

Reset the cursor used by :meth:next so the next call starts from the first item.

Source code in src/mzmlpy/lookup.py
 99
100
101
def reset(self) -> None:
    """Reset the cursor used by :meth:`next` so the next call starts from the first item."""
    self._cursor = None

mzmlpy.lookup.ChromatogramLookup

ChromatogramLookup(
    file_object: FileInterface,
    count: int | None = None,
    id_regex: str | None = None,
)

Bases: BaseLookup[Chromatogram]

Lookup interface for chromatograms.

Source code in src/mzmlpy/lookup.py
14
15
16
17
18
def __init__(self, file_object: FileInterface, count: int | None = None, id_regex: str | None = None) -> None:
    self.file_object = file_object
    self._count = count
    self._id_regex = id_regex
    self._cursor: Iterator[T] | None = None

TIC property

Access Total Ion Chromatogram.

count property

count: int | None

Get count of items.

get_by_index

get_by_index(index: int | str) -> T

Get item by index. Negative indices count from the end, like a list.

Source code in src/mzmlpy/lookup.py
39
40
41
42
43
44
45
46
47
48
def get_by_index(self, index: int | str) -> T:
    """Get item by index. Negative indices count from the end, like a list."""
    if isinstance(index, str):
        index = int(index)
    if index < 0:
        # Normalize against the count so lookup[-1] mirrors slice behavior (lookup[-1:]).
        count = self.count
        if count is not None:
            index += count
    return self._get_by_index_impl(index)

get_by_id

get_by_id(identifier: str) -> T

Get item by ID.

Source code in src/mzmlpy/lookup.py
50
51
52
def get_by_id(self, identifier: str) -> T:
    """Get item by ID."""
    return self._get_by_id_impl(identifier)

get_by_slice

get_by_slice(slice_obj: slice) -> list[T]

Get items by slice notation.

Source code in src/mzmlpy/lookup.py
54
55
56
57
58
59
60
61
62
def get_by_slice(self, slice_obj: slice) -> list[T]:
    """Get items by slice notation."""
    if self.count is None:
        # Don't know count - must iterate all and slice
        items: list[T] = list(self)
        return items[slice_obj]
    # Know count - use slice.indices() to handle all cases
    start, stop, step = slice_obj.indices(self.count)
    return [self.get_by_index(i) for i in range(start, stop, step)]

next

next() -> T

Advance a persistent cursor and return the next item.

The cursor is created on first call and advances on each subsequent call, raising StopIteration once every item has been returned. Call :meth:reset to start over.

Source code in src/mzmlpy/lookup.py
89
90
91
92
93
94
95
96
97
def next(self) -> T:
    """Advance a persistent cursor and return the next item.

    The cursor is created on first call and advances on each subsequent call, raising
    ``StopIteration`` once every item has been returned. Call :meth:`reset` to start over.
    """
    if self._cursor is None:
        self._cursor = iter(self)
    return next(self._cursor)

reset

reset() -> None

Reset the cursor used by :meth:next so the next call starts from the first item.

Source code in src/mzmlpy/lookup.py
 99
100
101
def reset(self) -> None:
    """Reset the cursor used by :meth:`next` so the next call starts from the first item."""
    self._cursor = None