try:
    last_indent_index = last_input.rindex('    ')
except:
    last_indent = 0
else:
    last_indent = int(last_indent_index / 4) + 1
if len(last_input.strip()) > 1:
    if last_input.count('(') > last_input.count(')'):
        indent = ''.join(['    ' for n in range(last_indent + 2)])
elif last_input.count(')') > last_input.count('('):
    indent = ''.join(['    ' for n in range(last_indent - 1)])
elif last_input.count('[') > last_input.count(']'):
    indent = ''.join(['    ' for n in range(last_indent + 2)])
elif last_input.count(']') > last_input.count('['):
    indent = ''.join(['    ' for n in range(last_indent - 1)])
elif last_input.count('{') > last_input.count('}'):
    indent = ''.join(['    ' for n in range(last_indent + 2)])
elif last_input.count('}') > last_input.count('{'):
    indent = ''.join(['    ' for n in range(last_indent - 1)])
elif last_input[-1] == ':':
    indent = ''.join(['    ' for n in range(last_indent + 1)])
else:
    indent = ''.join(['    ' for n in range(last_indent)])
readline.insert_text(indent)
readline.set_pre_input_hook(rl_autoindent)
Как включить автозаполнение python repl и разрешить новые вкладки строк