# -*-mode:pythonrc-*- vim:ft=python

# ~/.pythonrc
# =============================================================================
# Improve on the default Python interactive shell experience.
#
# Sources:
#   https://collingrady.wordpress.com/2006/11/17/tab-complete-and-a-persistent-history-in-python/
#   https://stackoverflow.com/questions/12334316/give-the-python-terminal-a-persistent-history

import atexit
import os

historyPath = os.path.expanduser('~/.pyhistory')

try:
    import readline
except ImportError:
    print('Module readline not available.')
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")
    if os.path.exists(historyPath):
        readline.read_history_file(historyPath)

def save_history(historyPath=historyPath):
    try:
        import readline
    except ImportError:
        print('Module readline not available.')
    else:
        readline.write_history_file(historyPath)

atexit.register(save_history)
