Add my old PYTHONSTARTUP file
- ID
2c9f4b6- date
2024-02-21 20:38:54+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
2364ab2- message
Add my old PYTHONSTARTUP file- changed files
2 files, 55 additions
Changed files
config.fish (6807) → config.fish (7114)
diff --git a/config.fish b/config.fish
index b0e8647..c365aac 100644
--- a/config.fish
+++ b/config.fish
@@ -76,6 +76,15 @@ prepend_to_path ~/repos/ttml2srt
set -g -x PIP_REQUIRE_VIRTUALENV true
+# This points to a file which will be run as a setup script any time
+# I start an interactive Python session. It customises the prompt slightly
+# and adds tab completion.
+#
+# See https://docs.python.org/3/using/cmdline.html#envvar-PYTHONSTARTUP
+#
+set -g -x PYTHONSTARTUP ~/repos/scripts/pythonstartup.py
+
+
# This tells fish to run a couple of functions as event handlers --
# that is, to run a function when a variable changes or something similar.
# These functions can't be autoloaded.
pythonstartup.py (0) → pythonstartup.py (1055)
diff --git a/pythonstartup.py b/pythonstartup.py
new file mode 100644
index 0000000..8bb5c76
--- /dev/null
+++ b/pythonstartup.py
@@ -0,0 +1,46 @@
+"""
+Improve the Python REPL.
+
+Inspired by this tweet by @nedbat:
+https://twitter.com/nedbat/status/817827164443840512
+
+Features:
+
+* Use pprint() by default.
+ https://gist.github.com/chekunkov/848c3472d4b0bee69bccd2e77907a590
+* Blue chevrons
+ http://www.jasonamyers.com/2017/default-to-pprint-python-repl/
+* Tab completion
+ https://github.com/patrik-johansson/dotfiles/blob/master/.pythonstartup
+
+"""
+
+import pprint
+import readline
+import rlcompleter
+import sys
+
+
+def displayhook_pprint(o):
+ """Display hook powered by pprint.
+ https://www.python.org/dev/peps/pep-0217/
+ """
+ if o is None:
+ return
+ if sys.version_info[0] == 2:
+ import __builtin__ as builtins
+ else:
+ import builtins
+ # Set '_' to None to avoid recursion
+ # https://docs.python.org/3/library/sys.html#sys.displayhook
+ builtins._ = None
+ pprint.pprint(o)
+ builtins._ = o
+
+
+sys.ps1 = "\033[0;34m>>> \033[0m"
+sys.ps2 = "\033[1;34m... \033[0m"
+
+sys.displayhook = displayhook_pprint
+
+readline.parse_and_bind("tab: complete")