Skip to content

Commit 24f6008

Browse files
penpenpngkyuridenamida
authored andcommitted
pythonコーディング規約に沿ってgeneratorを変更 (kyuridenamida#144)
* pythonコーディング規約に沿ってgeneratorを変更 * テストを追加 * pep8に従ってスタイルを修正
1 parent 4025164 commit 24f6008

File tree

12 files changed

+81
-37
lines changed

12 files changed

+81
-37
lines changed

atcodertools/codegen/code_generators/python.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
from typing import Dict, Any, Optional, List
2+
import re
23

34
from atcodertools.codegen.code_style_config import CodeStyleConfig
45
from atcodertools.codegen.models.code_gen_args import CodeGenArgs
56
from atcodertools.codegen.template_engine import render
6-
from atcodertools.fmtprediction.models.format import Pattern, SingularPattern, ParallelPattern, TwoDimensionalPattern, \
7-
Format
7+
from atcodertools.fmtprediction.models.format import (
8+
Pattern,
9+
SingularPattern,
10+
ParallelPattern,
11+
TwoDimensionalPattern,
12+
Format)
813
from atcodertools.fmtprediction.models.type import Type
914
from atcodertools.fmtprediction.models.variable import Variable
1015

@@ -19,8 +24,18 @@ def _loop_header(var: Variable, for_second_index: bool):
1924

2025
return "for {loop_var} in range({length}):".format(
2126
loop_var=loop_var,
22-
length=index.get_length()
23-
)
27+
length=_insert_space_around_operators(index.get_length()))
28+
29+
30+
def _insert_space_around_operators(code):
31+
code = str(code)
32+
precode = code
33+
pattern = r"([0-9a-zA-Z_])([+\-\*/])([0-9a-zA-Z_])"
34+
code = re.sub(pattern, r"\1 \2 \3", code)
35+
while precode != code:
36+
precode = code
37+
code = re.sub(pattern, r"\1 \2 \3", code)
38+
return code
2439

2540

2641
class Python3CodeGenerator:
@@ -99,14 +114,16 @@ def _generate_declaration(self, var: Variable):
99114
if len(dims) == 0:
100115
ctor = "{}()".format(ctype)
101116
elif len(dims) == 1:
102-
ctor = "[{ctype}()] * ({dim})".format(ctype=ctype, dim=dims[0])
117+
ctor = "[{ctype}()] * ({dim})".format(
118+
ctype=ctype, dim=_insert_space_around_operators(dims[0]))
103119
else:
104-
ctor = "[{ctype}()] * ({dim})".format(ctype=ctype, dim=dims[0])
120+
ctor = "[{ctype}()] * ({dim})".format(
121+
ctype=ctype, dim=_insert_space_around_operators(dims[0]))
105122
for dim in dims[-2::-1]:
106123
ctor = "[{ctor} for _ in range({dim})]".format(
107-
ctor=ctor, dim=dim)
124+
ctor=ctor, dim=_insert_space_around_operators(dim))
108125

109-
line = "{name} = {constructor} # type: {decl_type} ".format(
126+
line = "{name} = {constructor} # type: {decl_type}".format(
110127
name=var.name,
111128
decl_type=self._get_declaration_type(var),
112129
constructor=ctor
@@ -131,15 +148,17 @@ def _input_code_for_single_pattern(self, pattern: Pattern) -> str:
131148
input_ = self._input_code_for_token(var.type)
132149

133150
elif isinstance(pattern, ParallelPattern):
134-
input_ = "[ {input_} for _ in range({length}) ]".format(
151+
input_ = "[{input_} for _ in range({length})]".format(
135152
input_=self._input_code_for_token(var.type),
136-
length=var.first_index.get_length())
153+
length=_insert_space_around_operators(var.first_index.get_length()))
137154

138155
elif isinstance(pattern, TwoDimensionalPattern):
139-
input_ = "[ [ {input_} for _ in range({second_length}) ] for _ in range({first_length}) ]".format(
156+
input_ = "[[{input_} for _ in range({second_length})] for _ in range({first_length})]".format(
140157
input_=self._input_code_for_token(var.type),
141-
first_length=var.first_index.get_length(),
142-
second_length=var.second_index.get_length())
158+
first_length=_insert_space_around_operators(
159+
var.first_index.get_length()),
160+
second_length=_insert_space_around_operators(
161+
var.second_index.get_length()))
143162

144163
else:
145164
raise NotImplementedError

atcodertools/tools/templates/default_template.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
{% if prediction_success %}
33
import sys
44
{% endif %}
5+
{% if mod or yes_str or no_str %}
56

7+
{% endif %}
68
{% if mod %}
79
MOD = {{ mod }} # type: int
810
{% endif %}
@@ -12,13 +14,14 @@
1214
{% if no_str %}
1315
NO = "{{ no_str }}" # type: str
1416
{% endif %}
15-
1617
{% if prediction_success %}
18+
19+
1720
def solve({{ formal_arguments }}):
1821
return
19-
2022
{% endif %}
2123

24+
2225
# Generated by {{ atcodertools.version }} {{ atcodertools.url }} (tips: You use the default template now. You can remove this line by using your custom template)
2326
def main():
2427
{% if prediction_success %}

tests/resources/test_codegen/template.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python3
2+
import sys
3+
24

35
def solve(${formal_arguments}):
46
return

tests/resources/test_codegen/template_jinja.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
{% if prediction_success %}
33
import sys
44
{% endif %}
5+
{% if mod or yes_str or no_str %}
56

7+
{% endif %}
68
{% if mod %}
79
MOD = {{ mod }} # type: int
810
{% endif %}
@@ -12,13 +14,14 @@
1214
{% if no_str %}
1315
NO = "{{ no_str }}" # type: str
1416
{% endif %}
15-
1617
{% if prediction_success %}
18+
19+
1720
def solve({{ formal_arguments }}):
1821
return
19-
2022
{% endif %}
2123

24+
2225
def main():
2326
{% if prediction_success %}
2427
def iterate_tokens():

tests/resources/test_codegen/test_default_code_generators_and_templates/python/echo_template.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env python3
2+
{% if prediction_success %}
23
import sys
4+
{% endif %}
5+
{% if mod or yes_str or no_str %}
36

7+
{% endif %}
48
{% if mod %}
59
MOD = {{ mod }} # type: int
610
{% endif %}
@@ -10,6 +14,8 @@
1014
{% if no_str %}
1115
NO = "{{ no_str }}" # type: str
1216
{% endif %}
17+
{% if prediction_success %}
18+
1319

1420
def solve({{ formal_arguments }}):
1521
print(N, M)
@@ -29,6 +35,7 @@ def solve({{ formal_arguments }}):
2935
print(YES)
3036
print(NO)
3137
print(MOD)
38+
{% endif %}
3239

3340

3441
def main():

tests/resources/test_codegen/test_default_code_generators_and_templates/python/expected_default_generated_code.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
YES = "yes" # type: str
66
NO = "NO" # type: str
77

8+
89
def solve(N: int, M: int, H: "List[List[str]]", A: "List[int]", B: "List[float]", Q: int, X: "List[int]"):
910
return
1011

@@ -18,14 +19,14 @@ def iterate_tokens():
1819
tokens = iterate_tokens()
1920
N = int(next(tokens)) # type: int
2021
M = int(next(tokens)) # type: int
21-
H = [ [ next(tokens) for _ in range(M-1-2+1) ] for _ in range(N-2+1) ] # type: "List[List[str]]"
22-
A = [int()] * (N-2+1) # type: "List[int]"
23-
B = [float()] * (N-2+1) # type: "List[float]"
24-
for i in range(N-2+1):
22+
H = [[next(tokens) for _ in range(M - 1 - 2 + 1)] for _ in range(N - 2 + 1)] # type: "List[List[str]]"
23+
A = [int()] * (N - 2 + 1) # type: "List[int]"
24+
B = [float()] * (N - 2 + 1) # type: "List[float]"
25+
for i in range(N - 2 + 1):
2526
A[i] = int(next(tokens))
2627
B[i] = float(next(tokens))
2728
Q = int(next(tokens)) # type: int
28-
X = [ int(next(tokens)) for _ in range(M+Q) ] # type: "List[int]"
29+
X = [int(next(tokens)) for _ in range(M + Q)] # type: "List[int]"
2930
solve(N, M, H, A, B, Q, X)
3031

3132
if __name__ == '__main__':

tests/resources/test_codegen/test_default_code_generators_and_templates/python/expected_echo_generated_code.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
YES = "yes" # type: str
66
NO = "NO" # type: str
77

8+
89
def solve(N: int, M: int, H: "List[List[str]]", A: "List[int]", B: "List[float]", Q: int, X: "List[int]"):
910
print(N, M)
1011
assert len(H) == N - 1
@@ -33,14 +34,14 @@ def iterate_tokens():
3334
tokens = iterate_tokens()
3435
N = int(next(tokens)) # type: int
3536
M = int(next(tokens)) # type: int
36-
H = [ [ next(tokens) for _ in range(M-1-2+1) ] for _ in range(N-2+1) ] # type: "List[List[str]]"
37-
A = [int()] * (N-2+1) # type: "List[int]"
38-
B = [float()] * (N-2+1) # type: "List[float]"
39-
for i in range(N-2+1):
37+
H = [[next(tokens) for _ in range(M - 1 - 2 + 1)] for _ in range(N - 2 + 1)] # type: "List[List[str]]"
38+
A = [int()] * (N - 2 + 1) # type: "List[int]"
39+
B = [float()] * (N - 2 + 1) # type: "List[float]"
40+
for i in range(N - 2 + 1):
4041
A[i] = int(next(tokens))
4142
B[i] = float(next(tokens))
4243
Q = int(next(tokens)) # type: int
43-
X = [ int(next(tokens)) for _ in range(M+Q) ] # type: "List[int]"
44+
X = [int(next(tokens)) for _ in range(M + Q)] # type: "List[int]"
4445
solve(N, M, H, A, B, Q, X)
4546

4647
if __name__ == '__main__':

tests/resources/test_codegen/test_float_case/python/generated_code.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python3
2+
import sys
3+
24

35
def solve(L: int, N: int, M: int, K: "List[float]", A: "List[int]", S: "List[float]"):
46
return
@@ -13,9 +15,9 @@ def main():
1315
L = int(next(tokens)) # type: int
1416
N = int(next(tokens)) # type: int
1517
M = int(next(tokens)) # type: int
16-
K = [ float(next(tokens)) for _ in range(L) ] # type: "List[float]"
17-
A = [int()] * (N) # type: "List[int]"
18-
S = [float()] * (N) # type: "List[float]"
18+
K = [float(next(tokens)) for _ in range(L)] # type: "List[float]"
19+
A = [int()] * (N) # type: "List[int]"
20+
S = [float()] * (N) # type: "List[float]"
1921
for i in range(N):
2022
A[i] = int(next(tokens))
2123
S[i] = float(next(tokens))

tests/resources/test_codegen/test_long_case/python/generated_code.txt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python3
2+
import sys
3+
24

35
def solve(H: int, W: int, K: int, sr: int, sc: int, s: "List[str]", N: int, fr: "List[int]", fc: "List[int]", F: "List[int]", D: "List[int]"):
46
return
@@ -15,12 +17,12 @@ def main():
1517
K = int(next(tokens)) # type: int
1618
sr = int(next(tokens)) # type: int
1719
sc = int(next(tokens)) # type: int
18-
s = [ next(tokens) for _ in range(H) ] # type: "List[str]"
20+
s = [next(tokens) for _ in range(H)] # type: "List[str]"
1921
N = int(next(tokens)) # type: int
20-
fr = [int()] * (N) # type: "List[int]"
21-
fc = [int()] * (N) # type: "List[int]"
22-
F = [int()] * (N) # type: "List[int]"
23-
D = [int()] * (N) # type: "List[int]"
22+
fr = [int()] * (N) # type: "List[int]"
23+
fc = [int()] * (N) # type: "List[int]"
24+
F = [int()] * (N) # type: "List[int]"
25+
D = [int()] * (N) # type: "List[int]"
2426
for i in range(N):
2527
fr[i] = int(next(tokens))
2628
fc[i] = int(next(tokens))

tests/resources/test_codegen/test_mod_case/python/generated_code.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import sys
33

44
MOD = 998244353 # type: int
55

6+
67
def solve(A: int, B: int):
78
return
89

0 commit comments

Comments
 (0)