如何在IPython中优雅地预处理代码?

don*_*lon 5 python ipython

主要问题是-如何以最佳方式对IPython控制台中的行输入进行自定义处理?

IPython控制台嵌入在我的Python应用程序中。在我的应用程序中,我有基于的旧版本的控制台code.InteractiveConsole。方法中有一个处理InteractiveConsole.raw_input,它可以工作。IPython中的模拟物在哪里?

input_transformers_cleanup查看文档,发现对我的目标看起来不错。但是如何使用呢?我的调用get_ipython()返回None,但我不确定这是正确的方法。

谢谢。

don*_*lon 0

好的,这是我的模式:

import IPython
# IPython.start_ipython(config=c)

from traitlets import Type
from IPython.terminal.interactiveshell import TerminalInteractiveShell

class MyTerminalInteractiveShell(TerminalInteractiveShell):
    def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True):
        print("Override successful!!!", raw_cell)
        super(MyTerminalInteractiveShell, self).run_cell(raw_cell, store_history, silent, shell_futures)

class MyTerminalIPythonApp(IPython.terminal.ipapp.TerminalIPythonApp):
    interactive_shell_class = Type(
        klass=object,   # use default_value otherwise which only allow subclasses.
        default_value=MyTerminalInteractiveShell,
        help="Class to use to instantiate the TerminalInteractiveShell object. Useful for custom Frontends"
    ).tag(config=True)

app_class = MyTerminalIPythonApp
app = app_class.instance(config=c)
app.initialize()
app.start()
Run Code Online (Sandbox Code Playgroud)