views:

90

answers:

3

I'm trying to save myself just a few keystrokes for a command I type fairly regularly in Python.

In my python startup script, I define a function called load which is similar to import, but adds some functionality. 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.

I attempted to get around this by using the InteractivConsole and creating an instance of it in my startup file, like so:

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.

Is there a way to do this without writing a custom C program and embedding the interpreter into it?

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.

A: 

I think you want the cmd module.

See a tutorial here: http://wiki.python.org/moin/CmdModule

mcpeterson
I do not. I want to type "python" and have the normal shell with a small amount of extension.
Conspicuous Compiler
To be clear, the cmd modules is designed to make an entirely new interactive shell. Even if I were to implement an entirely new shell into it, I would have the same capabilities (and problem) that I have with the solution I posted in my question.
Conspicuous Compiler
Apologies. Should have read your question a little bit closer.
mcpeterson
+7  A: 

You could try ipython - which gives a python shell which does allow many things including automatic parentheses which gives you the function call as you requested.

Mark
+1; I've got bad habit of using iPython ;-) lol
Tumbleweed
+1. I think `ipython` gives you some of the features you're looking for.
Noufal Ibrahim
From the quick skim I did of the iPython website, I wasn't able to determine which version of Python it implements/wraps. I have to write code for 2.6/2.7/3 at various times, so I run python, python2.7, python3, and each binary conveniently reads the same startup file. Is there a way to change the version of Python that iPython uses? (Pardon me for veering a fair bit off the original question.)
Conspicuous Compiler
You can run ipython with various interpreters. It is not tied to any specific python implementation http://ipython.scipy.org/moin/FAQ (not sure about 3.0 though).
jimbob
@Mark: Oh, erp, nm. [The introduction](http://ipython.scipy.org/doc/manual/html/overview.html) states "works with either Python 2.5 or 2.6...We have not yet begun to test and port IPython to Python 3." This won't work for me.
Conspicuous Compiler
My Comment was wrong. Ipython is officially only tied to 2.5 or 2.6, but can be used with either. http://ipython.scipy.org/moin/
jimbob