Python Tips and Tricks

What's in my .pythonstartup?

Not much. The current contents are:
from pprint import pprint as pp
from see import see
import numpy as np

If you haven't already, make sure to set your PYTHONSTARTUP environment variable to point to ~/.pythonstartup.

However, I use IPython as my python shell. Prior to IPython 0.11, you can get IPython to use your .pythonstartup by adding the following two lines to your ~/.ipython/ipythonrc:
execute import os
execute execfile(os.environ['PYTHONSTARTUP'])
You may have noticed this nonstandard see module. see is an awesome alternative to python's built-in dir function, which I highly recommend.

I hate making all my GUI callback functions launch new threads!

Python decorators make this super easy. The following decorator will make any function run in its own thread.
from threading import Thread
from functools import wraps

def thread(func):
	def wrap(*args, **kwargs):
		t = Thread(target=func, args=args, kwargs=kwargs)
		return t	
	return wrap
Careful though. If any callbacks access global or class variables, you may start running into synchronization issues. In certain situations, python's synchronized queue class can help. Otherwise, RLocks can be helpful.