Skip to content

Commit 8ce0f3f

Browse files
Exclude all IPython special syntax (#23)
1 parent 33ef2aa commit 8ce0f3f

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/access_py_telemetry/ast.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from typing import Any
77
import ast
8+
import re
89
from IPython.core.getipython import get_ipython
910
from IPython.core.interactiveshell import ExecutionInfo
1011

@@ -17,6 +18,8 @@
1718

1819
registries = {registry: TelemetryRegister(registry) for registry in REGISTRIES.keys()}
1920

21+
IPYTHON_MAGIC_PATTERN = r"^\s*[%!?]{1,2}"
22+
2023

2124
def capture_registered_calls(info: ExecutionInfo) -> None:
2225
"""
@@ -39,7 +42,7 @@ def capture_registered_calls(info: ExecutionInfo) -> None:
3942

4043
# Remove lines that contain IPython magic commands
4144
code = "\n".join(
42-
line for line in code.splitlines() if not line.strip().startswith("%")
45+
line for line in code.splitlines() if not re.match(IPYTHON_MAGIC_PATTERN, line)
4346
)
4447

4548
tree = ast.parse(code)

0 commit comments

Comments
 (0)