Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ipynb ipython parser for magic and system #88

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ insert.py
build/
dist/
.venv*
.vscode/
35 changes: 34 additions & 1 deletion pigar/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@
from .helpers import parse_git_config, trim_suffix

import nbformat
import warnings

try:
import IPython
if IPython.__version__.split('.')[0] >= 7:
_transformer_manager = IPython.core.inputtransformer2.TransformerManager()
_input_transformer = None
elif IPython.__version__.split('.')[0] >= 5:
# TODO(yasirroni): support IPython 5 LTS ()
warning_msg = """Currently only support IPython >= 7.0"""
warnings.warn(warning_msg, Warning)
_transformer_manager = None
_input_transformer = None
else:
_transformer_manager = None
_input_transformer = None

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means that the current transformer (parser) only supports IPython > 6, and someone needs to add functionality to support IPython 5 LTS that is used by Google Colab.

I don't have IPython 5 LTS to test it and I don't use it anyway. So, Installing all the dependecy from scratch and make virtual environment is too much hassle (furthermore, I can't run your unittest).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of them seems to be an issue to me, also to you.

You can run the tests like that:

pigar/makefile

Lines 10 to 11 in 823a3c2

run-tests:
python -m unittest discover pigar/tests/ -t . -v

Copy link
Author

@yasirroni yasirroni Feb 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using IPython 7.0:

======================================================================
FAIL: test_py3_requirements (pigar.tests.test_core.ReqsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "P:\GitHub\pigar\pigar\tests\test_core.py", line 64, in test_py3_requirements
    self.assertListEqual(sorted(guess.keys()), ['foobar'])
AssertionError: Lists differ: ['foobar', 'subbar', 'subfoo'] != ['foobar']

First list contains 2 additional elements.
First extra element 1:
'subbar'

- ['foobar', 'subbar', 'subfoo']
+ ['foobar']

----------------------------------------------------------------------

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, for IPython 5.0 LTS: transformer

I haven't figured out how to use it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except Exception:
_transformer_manager = None
_input_transformer = None


Module = collections.namedtuple('Module', ['name', 'try_', 'file', 'lineno'])

Expand Down Expand Up @@ -61,7 +82,19 @@ def _read_code(fpath):
code = ""
for cell in nb.cells:
if cell.cell_type == "code":
code += cell.source + "\n"
if _transformer_manager:
code += _transformer_manager.transform_cell(cell.source)
elif _input_transformer:
# TODO(yasirroni):
# code += _input_transformer.push()
pass
else:
# allow without !magic and !system support
warning_msg = """No IPython >= 7.0
Not using IPython to parse notebooks
"""
warnings.warn(warning_msg, Warning)
code += cell.source + "\n"
yasirroni marked this conversation as resolved.
Show resolved Hide resolved
return code
elif fpath.endswith(".py"):
with open(fpath, 'rb') as f:
Expand Down
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,7 @@
entry_points={'console_scripts': [
'pigar=pigar.__main__:main',
]},
extras_require={
'ipython': ['IPython>=7.0.0']
}
)