Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Last active January 12, 2020 03:17
Show Gist options
  • Save stephenmm/4b1c4d9403513bd5535bad3818ab91ee to your computer and use it in GitHub Desktop.
Save stephenmm/4b1c4d9403513bd5535bad3818ab91ee to your computer and use it in GitHub Desktop.
Simple history function like bash history but for python interactive/repl.
# Simple history function like bash history but for python interactive/repl.
def history( num=0 ):
import readline
len = readline.get_current_history_length()
if( num>0 ):
num=len-num
for i in range(num,len):
print( i,readline.get_history_item(i + 1) )
# Later attemps at the same thing: Cygwin setup of Python REPL
# Define the interactive config file:
# echo 'export PYTHONCONFIG=~/.pythonrc' >> ~/.bashrc
# Defined a simple one at first: ~/.pythonrc
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
readline.read_history_file(histfile)
except IOError:
pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter
# But decided to try ptpython instead: ~/.pythonrc
import sys
try:
from ptpython.repl import embed
except ImportError:
print("ptpython is not available: falling back to standard prompt")
else:
sys.exit(embed(globals(), locals()))
# The ptpython one seems to be working well... So far.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment