gpt4 book ai didi

python - IPython检测自动魔术

转载 作者:行者123 更新时间:2023-12-04 11:29:44 25 4
gpt4 key购买 nike

如果我写

ls *.txt
放入 IPython 笔记本中的单元格中,然后它会正确执行。但是,如果我尝试使用 TransformerManager().transform_cell 转换单元格,没有任何 react ,我得到无效的 Python 语法:
>>> 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
有没有办法以返回有效 Python 代码的方式转换 automagics?没有 automagic 的等效代码将转换如下:
>>> TransformerManager().transform_cell('!ls *.txt')
"get_ipython().system('ls *.txt')\n"

我正在寻找的是一种无需运行代码即可检测自动魔术的方法

最佳答案

Automagics 是正在运行的内核的一个特性,而不是语法。例如, cd 本身就是一个有效的自动魔法,除非它被 Python 名称遮蔽,或者就此而言,如果 %automagic被禁用。

In [1]: cd
/home/wja

In [2]: cd = 'CD'

In [3]: cd
Out[3]: 'CD'

In [4]: del cd

In [5]: cd
/home/wja

In [6]: %automagic 0

Automagic is OFF, % prefix IS needed for line magics.

In [7]: cd
Traceback (most recent call last):
File "<ipython-input-7-9c6465b4471e>", line 1, in <module>
cd
NameError: name 'cd' is not defined
在幕后,据我所知,当一个单元格抛出某些错误时,例如 SyntaxErrorNameError ,它被送到前置过滤器,如果它可以转化为魔法,它就会被前置过滤器捕获 AutoMagicChecker 并转变。我的理解主要基于 this comment on an IPython GitHub issue :

Input transformers are applied line-by-line, but prefilters are only applied when the code is run. So 'invalid' [an invalid line] triggers an attempt to execute, and then prefilters step in and may transform it into valid code.


-- Thomas Kluyver , 2015 年 7 月 11 日
现在,如果您确实有一个正在运行的内核,则可以使用预过滤器,如下所示:
In [1]: ip = get_ipython()  # The running kernel

In [3]: source = ip.prefilter('cd') # Transform

In [4]: source
Out[4]: "get_ipython().run_line_magic('cd', '')"

In [5]: exec(source) # Run, just to prove it works
/home/wja
或者,很长的路要走:
In [2]: from IPython.core.splitinput import LineInfo

In [3]: line_info = LineInfo('cd') # Parse

In [4]: ip = get_ipython()

In [5]: ip.prefilter_manager.checkers # List of prefilters
Out[5]:
[<EmacsChecker(priority=100, enabled=False)>,
<MacroChecker(priority=250, enabled=True)>,
<IPyAutocallChecker(priority=300, enabled=True)>,
<AssignmentChecker(priority=600, enabled=True)>,
<AutoMagicChecker(priority=700, enabled=True)>,
<PythonOpsChecker(priority=900, enabled=True)>,
<AutocallChecker(priority=1000, enabled=True)>]

In [6]: for checker in ip.prefilter_manager.checkers:
...: handler = checker.check(line_info)
...: if handler: # Find the first one that matches
...: break
...:

In [7]: handler
Out[7]: <IPython.core.prefilter.MagicHandler at 0x7f01e8ccc7f0>

In [10]: handler.handle(line_info) # Transform
Out[10]: "get_ipython().run_line_magic('cd', '')"

关于python - IPython检测自动魔术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67960313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com