# -*- coding: utf-8 -*-

import rlcompleter
import readline
import atexit
import os
import sys
import pprint


# Enable tab completion.
# Source: https://docs.python.org/3/library/rlcompleter.html
readline.parse_and_bind("tab: complete")


# Enable history file.
# Source: https://docs.python.org/3/library/readline.html?highlight=readline#example
history_file = os.environ.get(
    "PYTHON_HISTORY_FILE",
    os.path.join(os.path.expanduser("~"), '.python_history'))
history_size = os.environ.get("PYTHON_HISTORY_SIZE", -1)

try:
    readline.read_history_file(history_file)
    history_length = readline.get_current_history_length()
except FileNotFoundError:
    open(history_file, 'wb').close()
    history_length = 0

def save(previous_length, history):
    new_length = readline.get_current_history_length()
    readline.set_history_length(history_size)
    readline.append_history_file(new_length - previous_length, history)

atexit.register(save, history_length, history_file)


# Enable pretty printing for stdout.
def display_hook(value):
    """ Pretty-print provided value. """
    if value is not None:
        try:
            import __builtin__
            __builtin__._ = value
        except ImportError:
            __builtins__._ = value
        pprint.pprint(value)
sys.displayhook = display_hook


print(
    'Persistent session history, tab completion and pretty printing are'
    ' enabled.')

@@ -29,85 +29,84 @@
 ort 
-rlcompleter%0Aimport readline%0Aimport atexit%0Aimport os%0Aimport sys%0Aimport pprint%0A
+atexit%0Aimport os%0Aimport pprint%0Aimport readline%0Aimport rlcompleter%0Aimport sys
 %0A%0A# 
@@ -397,20 +397,16 @@
 Y_FILE%22,
-%0A   
  os.path
@@ -436,17 +436,17 @@
 r(%22~%22), 
-'
+%22
 .python_
@@ -452,18 +452,19 @@
 _history
-')
+%22)%0A
 )%0Ahistor
@@ -677,12 +677,12 @@
 le, 
-'wb'
+%22wb%22
 ).cl
@@ -711,16 +711,17 @@
 th = 0%0A%0A
+%0A
 def save
@@ -922,16 +922,17 @@
 story)%0A%0A
+%0A
 atexit.r
@@ -1046,17 +1046,16 @@
 %0A    %22%22%22
- 
 Pretty-p
@@ -1074,17 +1074,16 @@
 d value.
- 
 %22%22%22%0A    
@@ -1148,16 +1148,17 @@
 iltin__%0A
+%0A
         
@@ -1275,16 +1275,18 @@
 (value)%0A
+%0A%0A
 sys.disp
@@ -1316,22 +1316,17 @@
 %0A%0Aprint(
-%0A    '
+%22
 Persiste
@@ -1387,15 +1387,11 @@
  are
-'%0A    '
+%22 %22
  ena
@@ -1399,7 +1399,7 @@
 led.
-'
+%22
 )%0A

Format Python code