Extending Python command line

Quinn Dunkan quinn at mark.ugcs.caltech.edu
Thu Jun 22 23:29:08 EDT 2000


On Fri, 23 Jun 2000 02:45:42 -0400, Andrew Henshaw
<andrew.henshaw at gtri.gatech.edu> wrote:
>Is there a reasonably simple way to extend the Python command line so that
>instead of typing:
>
>   >>> myspecialfunction('this is some stuff')
>
>I could type:
>
>   >>> myspecialfunction this is some stuff
>
>Everything else should be handled normally.

Sure (warning, completely untested code):

import string, sys, traceback
try:
    while 42 != 6 * 9:
        c = string.strip(raw_input(sys.ps1))
        if not c:
            continue
        if c[-1] == ':' or c[-1] == '\\':
            while 1:
                s = raw_input(sys.ps2)
                if not s:
                    c = c + '\n'
                    break
                if s[-1] == '\\':
                    c = c + ' ' + s[:-1]
                else:
                    c = c + '\n' + s
        special = 'myspecialfunction'
        if string.split(c)[0] == special:
            c = special + '(' + repr(c[len(special) + 1:]) + ')'
        try:
            print repr(eval(c))
        except SyntaxError:
            try:
                exec c
            except:
                traceback.print_exc()
        except:
            traceback.print_exc()
except:
    print

>A nice bonus would be if it could handle line continuations.

Depending on what that bonus is, I might give you an address to send it to.



More information about the Python-list mailing list