Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions scripts/test_tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ def test_notebook(self, path):
for cell in nb.get("cells"):
if cell["cell_type"] == "code":
src = cell["source"]
# Comment out lines containing '!' but not '!='.
src = re.sub(r"\!(?!=)", r"#!", src)
# Comment out lines with '!' or '%' (typically magic commands)
# but not '!='. Preserve indentation to avoid syntax errors.
lines = src.split('\n')
new_lines = []
for line in lines:
if re.match(r"^\s*(\!|%)(?!=)", line):
# Replace ! or % with 'pass # !' or 'pass # %'
match = re.search(r"(\!|%)", line)
idx = match.start()
new_lines.append(line[:idx] + "pass # " + line[idx:])
else:
new_lines.append(line)
src = '\n'.join(new_lines)

# For mnist.ipynb to reduce runtime in test.
src = re.sub(r"NUM_EXAMPLES ?= ?.*", "NUM_EXAMPLES = 10", src)
# For quantum_reinforcement_learning.ipynb:
Expand Down
Loading