Skip to content

Commit 50afe4a

Browse files
Auto-fix Linter and apply autopep8 changes
1 parent 771d41d commit 50afe4a

2 files changed

Lines changed: 57 additions & 25 deletions

File tree

restructuredpython/parser.py

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .check_syntax import check_syntax
1616
import re
1717

18+
1819
def wrap_loops_for_optimization(code):
1920
"""
2021
Rewrites for/while loops with <OPTIMIZE ...> annotations into runtime functions
@@ -42,7 +43,8 @@ def wrap_loops_for_optimization(code):
4243
if parallel and loop_line.startswith("for "):
4344
loop_match = re.match(r'for\s+(.*?)\s+in\s+(.*):?', loop_line)
4445
loop_vars = loop_match.group(1).strip() # type: ignore
45-
iter_expr = loop_match.group(2).strip().rstrip(':') # type: ignore
46+
iter_expr = loop_match.group(
47+
2).strip().rstrip(':') # type: ignore
4648
is_tuple_unpack = ',' in loop_vars
4749

4850
body_func_name = f"_repy_loop_body_{loop_counter}"
@@ -69,31 +71,42 @@ def wrap_loops_for_optimization(code):
6971
loop_counter += 1
7072

7173
modified_lines.append(" " * loop_indent + decorator_line)
72-
modified_lines.append(" " * loop_indent + f"def {body_func_name}({loop_vars}):")
74+
modified_lines.append(
75+
" " * loop_indent + f"def {body_func_name}({loop_vars}):")
7376
modified_lines.extend(loop_body)
7477

7578
# Emit executor function
76-
modified_lines.append(" " * loop_indent + f"def {func_name}():")
79+
modified_lines.append(
80+
" " * loop_indent + f"def {func_name}():")
7781
executor_type = "ThreadPoolExecutor"
78-
modified_lines.append(" " * (loop_indent + 4) + f"from concurrent.futures import {executor_type}")
82+
modified_lines.append(
83+
" " * (loop_indent + 4) + f"from concurrent.futures import {executor_type}")
7984

8085
if is_tuple_unpack:
81-
modified_lines.append(" " * (loop_indent + 4) + "def starmap_pool(fn, iterable):")
82-
modified_lines.append(" " * (loop_indent + 8) + f"with {executor_type}() as pool:")
83-
modified_lines.append(" " * (loop_indent + 12) + "futures = [pool.submit(fn, *args) for args in iterable]")
84-
modified_lines.append(" " * (loop_indent + 12) + "return [f.result() for f in futures]")
85-
modified_lines.append(" " * (loop_indent + 4) + f"starmap_pool({body_func_name}, {iter_expr})")
86+
modified_lines.append(
87+
" " * (loop_indent + 4) + "def starmap_pool(fn, iterable):")
88+
modified_lines.append(
89+
" " * (loop_indent + 8) + f"with {executor_type}() as pool:")
90+
modified_lines.append(
91+
" " * (loop_indent + 12) + "futures = [pool.submit(fn, *args) for args in iterable]")
92+
modified_lines.append(
93+
" " * (loop_indent + 12) + "return [f.result() for f in futures]")
94+
modified_lines.append(
95+
" " * (loop_indent + 4) + f"starmap_pool({body_func_name}, {iter_expr})")
8696
else:
87-
modified_lines.append(" " * (loop_indent + 4) + f"with {executor_type}() as pool:")
88-
modified_lines.append(" " * (loop_indent + 8) + f"list(pool.map({body_func_name}, {iter_expr}))")
97+
modified_lines.append(
98+
" " * (loop_indent + 4) + f"with {executor_type}() as pool:")
99+
modified_lines.append(
100+
" " * (loop_indent + 8) + f"list(pool.map({body_func_name}, {iter_expr}))")
89101

90102
# Call the executor function
91103
modified_lines.append(" " * loop_indent + f"{func_name}()")
92104

93105
else:
94106
# Non-parallel loop: wrap as usual
95107
modified_lines.append(" " * loop_indent + decorator_line)
96-
modified_lines.append(" " * loop_indent + f"def {func_name}():")
108+
modified_lines.append(
109+
" " * loop_indent + f"def {func_name}():")
97110
modified_lines.append(" " * (loop_indent + 4) + loop_line)
98111
i += 2
99112
loop_body = []
@@ -111,31 +124,38 @@ def wrap_loops_for_optimization(code):
111124
loop_body.append(" " * new_indent + body_line.lstrip())
112125
i += 1
113126

114-
if unroll_factor > 1 and loop_line.startswith("for ") and "range(" in loop_line:
127+
if unroll_factor > 1 and loop_line.startswith(
128+
"for ") and "range(" in loop_line:
115129
range_match = re.search(r'range\(([^)]+)\)', loop_line)
116130
if range_match:
117131
range_args = range_match.group(1).split(',')
118132
if len(range_args) == 1:
119-
start, end, step = "0", range_args[0].strip(), str(unroll_factor)
133+
start, end, step = "0", range_args[0].strip(), str(
134+
unroll_factor)
120135
elif len(range_args) == 2:
121-
start, end = range_args[0].strip(), range_args[1].strip()
136+
start, end = range_args[0].strip(
137+
), range_args[1].strip()
122138
step = str(unroll_factor)
123139
elif len(range_args) == 3:
124-
start, end, step = [arg.strip() for arg in range_args]
140+
start, end, step = [arg.strip()
141+
for arg in range_args]
125142
step = f"({step}) * {unroll_factor}"
126143

127-
var_match = re.match(r'for\s+(\w+)\s+in\s+range', loop_line)
144+
var_match = re.match(
145+
r'for\s+(\w+)\s+in\s+range', loop_line)
128146
loop_var = var_match.group(1) if var_match else "i"
129147

130148
new_loop_line = f"for {loop_var} in range({start}, {end}, {step}):"
131-
modified_lines[-1] = " " * (loop_indent + 4) + new_loop_line
149+
modified_lines[-1] = " " * \
150+
(loop_indent + 4) + new_loop_line
132151

133152
for offset in range(unroll_factor):
134153
for body in loop_body:
135154
if offset == 0:
136155
unrolled_line = body
137156
else:
138-
unrolled_line = re.sub(rf'\b{loop_var}\b', f"{loop_var}+{offset}", body)
157+
unrolled_line = re.sub(
158+
rf'\b{loop_var}\b', f"{loop_var}+{offset}", body)
139159
modified_lines.append(unrolled_line)
140160
else:
141161
modified_lines.extend(loop_body)
@@ -221,7 +241,8 @@ def nest(parts):
221241
required_imports.add("optimize_loop")
222242
pending_optimize = None
223243
elif re.match(r'^\s*def\s+.*\{', processed_line):
224-
modified_code.append(" " * loop_indent + f"@optimize_function({pending_optimize})")
244+
modified_code.append(
245+
" " * loop_indent + f"@optimize_function({pending_optimize})")
225246
required_imports.add("optimize_function")
226247
pending_optimize = None
227248

restructuredpython/predefined/subinterpreter/optimize.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
import multiprocessing
2020
import warnings
2121

22-
def optimize_loop(profile=False, gct=False, multithreading=False, parallel=False, cache=False, unroll=0):
22+
23+
def optimize_loop(
24+
profile=False,
25+
gct=False,
26+
multithreading=False,
27+
parallel=False,
28+
cache=False,
29+
unroll=0):
2330
"""
2431
Decorator to optimize loop-like functions.
2532
Supports profiling, garbage collection, parallel execution, caching, and JIT compilation.
@@ -41,7 +48,7 @@ def wrapper(*args, **kwargs):
4148
fn(*args, **kwargs)
4249

4350
if profile:
44-
duration = time.perf_counter() - start # type: ignore
51+
duration = time.perf_counter() - start # type: ignore
4552
print(f"[PROFILE] Loop took {duration:.4f}s")
4653

4754
return wrapper
@@ -63,8 +70,12 @@ def decorator(fn):
6370
@functools.wraps(original_fn)
6471
def profiled(*args, **kwargs):
6572
start = time.perf_counter()
66-
result = fn(*args, **kwargs) # type: ignore
67-
print(f"[PROFILE] {original_fn.__name__} took {time.perf_counter() - start:.4f}s")
73+
result = fn(*args, **kwargs) # type: ignore
74+
print(
75+
f"[PROFILE] {
76+
original_fn.__name__} took {
77+
time.perf_counter() -
78+
start:.4f}s")
6879
return result
6980
return profiled
7081

@@ -76,7 +87,7 @@ def tracer(frame, event, arg):
7687
@functools.wraps(original_fn)
7788
def traced(*args, **kwargs):
7889
sys.settrace(tracer)
79-
result = fn(*args, **kwargs) # type: ignore
90+
result = fn(*args, **kwargs) # type: ignore
8091
sys.settrace(None)
8192
return result
8293
return traced

0 commit comments

Comments
 (0)