If I write
ls *.txt
into a cell in an IPython notebook, then it correctly executes. However, if I try to transform the cell using TransformerManager().transform_cell, nothing happens, and I get invalid Python syntax:
>>> from IPython.core.inputtransformer2 import TransformerManager
>>> import ast
>>> TransformerManager().transform_cell('ls *.txt')
'ls *.txt\n'
>>> ast.parse('ls *.txt\n')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ignoring_gravity/miniconda3/envs/tmp/lib/python3.8/ast.py", line 47, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 1
ls *.txt
^
SyntaxError: invalid syntax
Is there a way to transform automagics in a way that returns valid Python code? The equivalent code, without the automagic, would get transformed as follows:
>>> TransformerManager().transform_cell('!ls *.txt')
"get_ipython().system('ls *.txt')\n"
What I'm looking for is a way of detecting automagics without running the code
from IPython detect automagics
No comments:
Post a Comment