-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipywalrus.py
78 lines (67 loc) · 2.1 KB
/
ipywalrus.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import ast
import sys
from io import BytesIO
from token import COMMENT, ENCODING, INDENT, NEWLINE, OP
from tokenize import tokenize, untokenize
def add_tokens_around(line):
start = True
spaces = "" # stores indentation
result = []
tokens = tokenize(BytesIO(line.encode("utf-8")).readline)
for token in tokens:
if token.type == ENCODING:
result.append((token.type, token.string))
continue
elif token.type == INDENT:
result.append((token.type, token.string))
spaces += token.string
continue
elif start:
result.append((OP, ("(")))
result.append((token.type, token.string))
start = False
elif token.type in (COMMENT, NEWLINE):
result.append((OP, (")")))
result.append((token.type, token.string))
else:
result.append((token.type, token.string))
return spaces + untokenize(result).decode("utf-8")
def wrap_walrus_assignment(lines):
code = "".join(lines)
# If original code does not raise SyntaxError
# there is no need to modify it
try:
ast.parse(code)
except SyntaxError as e:
original_exception = e
else:
return lines
new_lines = []
# For each line with walrus, add ()
for line in lines:
if ":=" in line:
new_lines.append(add_tokens_around(line))
else:
new_lines.append(lines)
new_code = "".join(new_lines)
try:
ast.parse(new_code)
except SyntaxError:
raise original_exception from None
else:
return new_lines
def load_ipython_extension(ipython):
if sys.version_info >= (3, 8):
ipython.input_transformers_post.append(wrap_walrus_assignment)
else:
print(
(
"Python {}.{} is too old. Walrus operator is available ",
"since Python 3.8".format(*sys.version_info[:2]),
)
)
def unload_ipython_extension(ipython):
try:
ipython.input_transformers_post.remove(wrap_walrus_assignment)
except ValueError:
pass