Last active
March 8, 2018 05:25
-
-
Save masamitsu-murase/88eec7a47467109b7962c80ee0e2e982 to your computer and use it in GitHub Desktop.
Interactive Shell for Python with pyreadline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@goto RUN_PYTHON | |
# -*- coding: UTF-8 -*- | |
""" | |
:RUN_PYTHON | |
@echo off | |
cd /d "%~dp0" | |
python -x -i "%~nx0" | |
exit /b %ERRORLEVEL% | |
""" | |
# from __future__ import print_function, unicode_literals, absolute_import | |
def main(): | |
import os.path | |
_history_file_name = os.path.join(os.path.abspath(os.path.dirname(__file__)), "history.txt") | |
_history_length = 2048 | |
def at_exit_callback(): | |
import readline | |
try: | |
readline.write_history_file(_history_file_name) | |
except IOError: | |
print("Failed to save history in %s." % _history_file_name) | |
try: | |
import pyreadline.rlmain | |
#pyreadline.rlmain.config_path=r"c:\xxx\pyreadlineconfig.ini" | |
import readline, atexit | |
import pyreadline.unicode_helper | |
# | |
# | |
#Normally the codepage for pyreadline is set to be sys.stdout.encoding | |
#if you need to change this uncomment the following line | |
#pyreadline.unicode_helper.pyreadline_codepage="utf8" | |
except ImportError: | |
print("Module readline not available.") | |
else: | |
#import tab completion functionality | |
import rlcompleter | |
#Override completer from rlcompleter to disable automatic ( on callable | |
completer_obj = rlcompleter.Completer() | |
def nop(val, word): | |
return word | |
completer_obj._callable_postfix = nop | |
readline.set_completer(completer_obj.complete) | |
#activate tab completion | |
readline.parse_and_bind("tab: complete") | |
readline.set_history_length(_history_length) | |
readline.read_history_file(_history_file_name) | |
atexit.register(at_exit_callback) | |
import sys | |
print("Python " + sys.version + " on " + sys.platform) | |
main() | |
del main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment