Related Entries

India PyCon 2009
Quick wallpaper changer
Load testing with Grinder
Adding namespace to XML
Opera RSS to OPML

« Jython zxJDBC rocks!
» Grasso Quits

Interactive Python - notes

Additional notes on what I like about Python's interactive interpreter.

Simon Willison has a great example, explaining Python’s interactive interpreter. I agree with Simon on how useful it is for rapid application development, by seeing results for each line, right there.

A really great feature is the -i option.

When called like python -i myfile.py, it executes myfile.py and remains in the prompt, instead of exiting. Very handy to do common initialization. Mostly, I use it for database operations. I’ve boiler plate code that opens a DB connection and attaches a cursor to it. So, when I start Python like python -i dbinit.py, I can straightaway start using DB-API calls.

There is also an environment variable setting called PYTHONSTARTUP. This is useful if you want to run some Python code always, before giving you the control.

I would really love to have a feature to save the commands I typed in as a Python file. IDLE saves it, but also adds the output. If you’ve sed here’s a quick shell pipe to comment the output and remove the prompts (>>>) - it won’t work if your output starts with tabs. It works for me for most of my needs - anything more complicated, I prefer using a decent editor.


sed ’s/^\t/>>> \t/g' saved_file.py|sed ’s/^\([^>>>]\)/#~ \1g'|sed ’s/^>>> //g'

Note: Simon’s blog entry has a comment that has one line of Python code for Pig Latin generation; that is more Perlish than Perl code :-) I need Pastis (or triple vodka martini) to figure that out.

//-->