Skip to content

Instantly share code, notes, and snippets.

@tritium21
Last active January 7, 2016 23:59
Show Gist options
  • Save tritium21/0d9bb4fb7ba1cf961729 to your computer and use it in GitHub Desktop.
Save tritium21/0d9bb4fb7ba1cf961729 to your computer and use it in GitHub Desktop.
def comp():
import readline
import sys
try:
# python >= 3.something
del sys.__interactivehook__
except AttributeError:
pass
try:
from jedi.utils import setup_readline
setup_readline()
except ImportError:
print("Jedi is not installed, falling back to readline")
try:
import rlcompleter
assert rlcompleter
readline.parse_and_bind("tab: complete")
except ImportError:
print("Readline is not installed. No tab completion is enabled.")
def history():
import os
import sys
import readline
# ancient pythons dont support history files?
if not hasattr(readline, 'write_history_file'):
return
def hpath():
try:
# python >= 2.3
import platform
impl = platform.python_implementation()
except ImportError:
# python < 2.3
impl = 'CPython'
try:
# python >= 2.0
major, minor = sys.version_info[:2]
except AttributeError:
# python < 2.0 ... no split on str objects, fun.
import string
major, minor = string.split(sys.version, '.')[:2]
hname = '~/.pyhistory-%s%s.%s' % (impl, major, minor)
return os.path.expanduser(hname)
historyPath = hpath()
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
try:
# python >= 2.0
import atexit
atexit.register(save_history)
except ImportError:
# should probably play nice here...
sys.exitfunc = save_history
if __name__ == '__main__':
comp()
history()
del comp, history
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment