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?

Re: Live doctest in TextMate (IPython + Twisted?)
posted by Chris McAvoy on Thursday Mar 1st, 2007 at 3:49p.m.
This is a great idea...I have no advice, just encouragement. Dozens of people using TextMate made me switch back from Emacs...at least for now. Auto-doctest completion would be a pretty damn cool thing.
Re: Live doctest in TextMate (IPython + Twisted?)
posted by Matt Good on Sunday Mar 4th, 2007 at 11:40p.m.
Well, it sounds interesting, but it seems limited to regression testing since the code would need to pass each test before you write it. This seems like it would get annoying if you're trying to do test-driven development since your tests will fail when you first write them. Getting stack-traces in the output every time you write a test would get frustrating quickly.
Re: Live doctest in TextMate (IPython + Twisted?)
posted by Kumar McMillan on Tuesday Mar 6th, 2007 at 5:11p.m.
damn, that's a really good point. hmm. I think I can only determine whether or not it's useful once I get it working. Even if it turns out to be useless, my current proof is a fun exploration of unix domain sockets!