farmdev

Live doctest in TextMate (IPython + Twisted?)

I use TextMate for all python dev and I've had this idea to make typing doctests yield live results. In other words, when you are editing a module, if you were in a doctest you would type a line of code and the next line of your module would contain the result.

Just imagine typing and getting back...

This is how you use the class::

    >>> foo = Barbaz()
    >>> if foo.babulated:
    ...     foo.unbabulate()
    ... 
    'foo is now unbabulated'
    >>>

...I dunno about you, but I think that'd be slick!

The implementation is pretty simple with IPython :

from IPython.iplib import InteractiveShell
s = InteractiveShell('shell', user_global_ns=globals())
s.push('class foo(object):')
s.push('    pass')
s.push('')
s.push('foo')
# <class '__main__.foo'>

...huzzah. In TextMate, you can execute a script on the <return> key and the script would get $TM_CURRENT_LINE, full text of the line you're on. However, the current framework would execute the script once each time you pressed return so there is a problem with shared memory (no record of the previous lines you fed into it).

This is the part I'm still trying to figure out. I could fork another process and make some kind of interface (REST, pyro?) for feeding it new lines. The shared namespaces could be hashed per file, using $TM_FILENAME, and could simply use timeouts to do cleanup. It would need some security—maybe a block of all non-local IPs + some kind of permission check per user, but this is all I can think of. Anyone know of a better way? Anyone interested in this kind of thing? I am getting a heavy feeling that I need to learn Twisted! Can anyone point me in a good direction for how to do this in Twisted?