Skip to content

Commit

Permalink
Added regex to exclude magic commands from ipynbs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmmn378 committed Dec 4, 2021
1 parent 2a4f66c commit c9d0f9c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pigar/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import print_function, division, absolute_import

import os
import re
import sys
import fnmatch
import ast
Expand Down Expand Up @@ -55,13 +56,22 @@ def parse_imports(package_root, ignores=None):
return imported_modules, user_modules


def _filter_ipynb_commands(cell_lines):
"""
Filter out cells with magics and shell commands
e.g %matplotlib inline or !pip install pigar
"""
return re.sub(r"^((\s)*(!|%).*$)|(\n\s*%.*)", "", cell_lines)


def _read_code(fpath):
if fpath.endswith(".ipynb"):
nb = nbformat.read(fpath, as_version=4)
code = ""
for cell in nb.cells:
if cell.cell_type == "code":
code += cell.source + "\n"
source = _filter_ipynb_commands(cell.source)
code += source + "\n"
return code
elif fpath.endswith(".py"):
with open(fpath, 'rb') as f:
Expand Down

0 comments on commit c9d0f9c

Please sign in to comment.