Python REPL

Even though there are improved variants like ipython, it’s still quite useable.

Pair it with your favourite text editor

You can type text in a file, send it to a live REPL, and avoid having to reload all your code every time you make a change.

How to exit: ^+D

Add some colors and pprint to it. My .pythonrc.py:

def colors():
    term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux',
                        'screen', 'screen-256color', 'screen-bce', 'tmux-256color']
    red = ''
    green = ''
    reset = ''
    import os
    import sys
    if os.environ.get('TERM') in term_with_colors:
        escapes_pattern = '\001\033[%sm\002'  # \001 and \002 mark non-printing
        red = escapes_pattern % '33'
        green = escapes_pattern % '32'
        reset = escapes_pattern % '0'
        sys.ps1 = green + '>>> ' + reset
        sys.ps2 = red + '... ' + reset

    def my_displayhook(value):
        if value is not None:
            try:
                import __builtin__
                __builtin__._ = value
            except ImportError:
                __builtins__._ = value

            import pprint
            pprint.pprint(value)

    orig_displayhook = sys.displayhook
    __builtins__.pprint_off = lambda: setattr(sys, 'displayhook', orig_displayhook)
    sys.displayhook = my_displayhook

colors()
del colors

For the adventurous: If you don’t like SLIME or you can’t find other ways to connect REPL with your edit, make .pythonrc.py automatically reload your files.

#python