In writing python code or perl in an editor, I often want to execute a tiny piece of the code in a standalone fashion as a way of debugging it. To facilitate this, I wrote a little perl script that allows me to execute any text selected in any Macintosh editor over in a Terminal window.
Copy and paste this script into a text editor, save it as python_execute.pl or perl_execute.pl, and make it executable (chmod a+x python_execute.pl). Edit the file as indicated to switch it from targeting perl to targeting python, as you prefer. To use it, select text in any editor, and select the Copy function from the editor's Edit menu. This loads the text into the clipboard (AKA the pasteboard). Then in any terminal window type:
pbpaste | python_execute.pl
And it will print the selected text to the window, save it to a temporary file (in /tmp), and then execute that file as a python file. The resulting error message, or any output from the execution, is printed to the Terminal window.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20050309190400797