简体   繁体   English

如何向Python交互式外壳添加命令?

[英]How can I add a command to the Python interactive shell?

I'm trying to save myself just a few keystrokes for a command I type fairly regularly in Python. 我试图为自己节省一些击键操作,而这只是我在Python中经常键入的命令。

In my python startup script , I define a function called load which is similar to import, but adds some functionality. 在我的python启动脚本中 ,我定义了一个名为load的函数,该函数类似于import,但是增加了一些功能。 It takes a single string: 它需要一个字符串:

def load(s):
  # Do some stuff
  return something

In order to call this function I have to type 为了调用此函数,我必须输入

>>> load('something')

I would rather be able to simply type: 我宁愿能够简单地键入:

>>> load something

I am running Python with readline support, so I know there exists some programmability there, but I don't know if this sort of thing is possible using it. 我正在运行具有readline支持的Python,所以我知道那里存在一些可编程性,但是我不知道使用它是否可以进行这种事情。

I attempted to get around this by using the InteractivConsole and creating an instance of it in my startup file, like so: 我试图通过使用InteractivConsole并在启动文件中创建它的实例来解决此问题,如下所示:

import code, re, traceback

class LoadingInteractiveConsole(code.InteractiveConsole):
  def raw_input(self, prompt = ""):
    s = raw_input(prompt)
    match = re.match('^load\s+(.+)', s)
    if match:
      module = match.group(1)
      try:
        load(module)
        print "Loaded " + module
      except ImportError:
        traceback.print_exc()
      return ''
    else:
      return s

console = LoadingInteractiveConsole()
console.interact("")

This works with the caveat that I have to hit Ctrl-D twice to exit the python interpreter: once to get out of my custom console, once to get out of the real one. 需要注意的是,我必须按两次Ctrl-D退出python解释器,这一点可以解决:一次退出我的自定义控制台,一次退出真正的控制台。

Is there a way to do this without writing a custom C program and embedding the interpreter into it? 有没有一种方法,而无需编写自定义C程序并将解释器嵌入其中呢?

Edit 编辑

Out of channel, I had the suggestion of appending this to the end of my startup file: 在频道外,我建议将其附加到启动文件的末尾:

import sys
sys.exit()

It works well enough, but I'm still interested in alternative solutions. 它运作良好,但是我仍然对替代解决方案感兴趣。

您可以尝试ipython-它提供了一个python shell,它确实允许很多事情,包括自动括号 ,该括号使您可以根据需要进行函数调用。

I think you want the cmd module. 我认为您想要cmd模块。

See a tutorial here: http://wiki.python.org/moin/CmdModule 在此处查看教程: http : //wiki.python.org/moin/CmdModule

Hate to answer my own question, but there hasn't been an answer that works for all the versions of Python I use. 讨厌回答我自己的问题,但是还没有一个答案适用于我使用的所有Python版本。 Aside from the solution I posted in my question edit (which is what I'm now using), here's another: 除了我在问题编辑中发布的解决方案(这是我现在正在使用的解决方案)之外,还有另一个:

Edit .bashrc to contain the following lines: 编辑.bashrc以包含以下行:

alias python3='python3 ~/py/shellreplace.py'
alias python='python ~/py/shellreplace.py'
alias python27='python27 ~/py/shellreplace.py'

Then simply move all of the LoadingInteractiveConsole code into the file ~/py/shellreplace.py Once the script finishes executing, python will cease executing, and the improved interactive session will be seamless. 然后只需将所有LoadingInteractiveConsole代码移动到文件~/py/shellreplace.py一旦脚本执行完毕,python将停止执行,并且改进的交互式会话将是无缝的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM