Skip to content

Utilities Module

The utilities module provides helper functions for detecting and classifying magic commands in cell content.

Functions

Utility functions for magic command detection.

get_line_magics_cached() cached

Return cached set of all registered line magic names.

Returns:

Type Description
FrozenSet[str]

Frozen set of magic command names (without % prefix).

Source code in jumper_wrapper_kernel/utilities.py
 9
10
11
12
13
14
15
16
17
@lru_cache(maxsize=1)
def get_line_magics_cached() -> FrozenSet[str]:
    """Return cached set of all registered line magic names.

    Returns:
        Frozen set of magic command names (without % prefix).
    """
    ip = get_ipython()
    return frozenset(ip.magics_manager.lsmagic().get("line", []))

is_known_line_magic(line, line_magics)

Check if a line starts with a known magic command.

Parameters:

Name Type Description Default
line str

Single line of code to check.

required
line_magics frozenset

Set of known magic names (without % prefix).

required

Returns:

Type Description
bool

True if line starts with %, False otherwise.

Source code in jumper_wrapper_kernel/utilities.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def is_known_line_magic(line: str, line_magics: frozenset) -> bool:
    """Check if a line starts with a known magic command.

    Args:
        line: Single line of code to check.
        line_magics: Set of known magic names (without % prefix).

    Returns:
        True if line starts with %<known_magic>, False otherwise.
    """
    s = line.lstrip()
    if not s.startswith("%"):
        return False
    name = s[1:].split(None, 1)[0]
    return name in line_magics

is_pure_line_magic_cell(raw_cell)

A pure line-magic cell = each non-empty line is either: - starts with % (optionally with arguments), - or is a comment (#...).

Source code in jumper_wrapper_kernel/utilities.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def is_pure_line_magic_cell(raw_cell: str) -> bool:
    """
    A pure line-magic cell = each non-empty line is either:
      - starts with %<known_magic> (optionally with arguments),
      - or is a comment (#...).
    """
    line_magics = get_line_magics_cached()
    lines = raw_cell.splitlines()
    for line in lines:
        stripped = line.strip()
        if not stripped:
            continue
        if stripped.startswith("#"):
            continue
        if is_known_line_magic(line, line_magics):
            continue
        return False
    return True

is_local_magic_cell(raw_cell, local_magics)

Check if cell contains only local magic commands (and comments/empty lines). Returns True if all non-empty, non-comment lines are local magics.

Source code in jumper_wrapper_kernel/utilities.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
def is_local_magic_cell(raw_cell: str, local_magics: set) -> bool:
    """
    Check if cell contains only local magic commands (and comments/empty lines).
    Returns True if all non-empty, non-comment lines are local magics.
    """
    lines = raw_cell.splitlines()
    has_magic = False
    for line in lines:
        stripped = line.strip()
        if not stripped:
            continue
        if stripped.startswith("#"):
            continue
        if stripped.startswith("%"):
            name = stripped[1:].split(None, 1)[0]
            if name in local_magics:
                has_magic = True
                continue
        return False
    return has_magic