Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
50 lines (35 loc) · 1.45 KB

File metadata and controls

50 lines (35 loc) · 1.45 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# encoding: utf-8
"""Tools for preparing code to be run in the REPL (removing blank lines,
etc)"""
from bpython.lazyre import LazyReCompile
# TODO specifically catch IndentationErrors instead of any syntax errors
indent_empty_lines_re = LazyReCompile(r'\s*')
tabs_to_spaces_re = LazyReCompile(r'^\t+')
def indent_empty_lines(s, compiler):
"""Indents blank lines that would otherwise cause early compilation
Only really works if starting on a new line"""
lines = s.split('\n')
ends_with_newline = False
if lines and not lines[-1]:
ends_with_newline = True
lines.pop()
result_lines = []
for p_line, line, n_line in zip([''] + lines[:-1], lines,
lines[1:] + ['']):
if len(line) == 0:
p_indent = indent_empty_lines_re.match(p_line).group()
n_indent = indent_empty_lines_re.match(n_line).group()
result_lines.append(min([p_indent, n_indent], key=len) + line)
else:
result_lines.append(line)
return '\n'.join(result_lines) + ('\n' if ends_with_newline else '')
def leading_tabs_to_spaces(s):
lines = s.split('\n')
result_lines = []
def tab_to_space(m):
return len(m.group()) * 4 * ' '
for line in lines:
result_lines.append(tabs_to_spaces_re.sub(tab_to_space, line))
return '\n'.join(result_lines)
def preprocess(s, compiler):
return indent_empty_lines(leading_tabs_to_spaces(s), compiler)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.