Last active
November 26, 2019 00:36
-
-
Save RobRuana/d64db6e01614cc09f02e to your computer and use it in GitHub Desktop.
Make your Python interactive shell infinitely more usable
This file contains 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
# .pythonstartup - executed by python interactive shell | |
import atexit | |
import os | |
import pprint | |
import readline | |
import rlcompleter | |
import sys | |
try: | |
import builtins | |
except ImportError: | |
builtins = __builtins__ | |
try: | |
from importlib import reload | |
except ImportError: | |
pass | |
try: | |
import asyncio | |
loop = asyncio.get_event_loop_policy().new_event_loop() | |
def run(coro): | |
return loop.run_until_complete(coro) | |
except Exception: | |
pass | |
# nicer output | |
orig_displayhook = sys.displayhook | |
def pprint_displayhook(value): | |
if value is not None: | |
builtins._ = value | |
pprint.pprint(value) | |
builtins.pprint_on = lambda: setattr(sys, 'displayhook', pprint_displayhook) | |
builtins.pprint_off = lambda: setattr(sys, 'displayhook', orig_displayhook) | |
pprint_on() | |
# tab completion | |
if 'libedit' in readline.__doc__: | |
readline.parse_and_bind("bind ^I rl_complete") | |
else: | |
readline.parse_and_bind("tab: complete") | |
readline.set_completer(rlcompleter.Completer(globals()).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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment