# Really clever trick to add history and tab completion to python
# Save this as ~/.pythonrc and add  PYTHONSTARTUP=~/.pythonrc to your .bashrc

try:
    import readline
except ImportError:
    pass
else:
    import os
    import atexit
    import rlcompleter

class irlcompleter(rlcompleter.Completer):
    def complete(self, text, state):
        if text == "":
            readline.insert_text('\t')
            return None
        else:
            return rlcompleter.Completer.complete(self,text,state)

# You could change this line to bind another key instead tab.
readline.parse_and_bind("tab: complete")
readline.set_completer(irlcompleter().complete)
# Restore our command-line history, and save it when Python exits.
historyPath = os.path.expanduser("~/.pyhistory")
readline.read_history_file(historyPath)
atexit.register(lambda x=historyPath: readline.write_history_file(x))
