Skip to content

Commit e4354c2

Browse files
committed
Parallelize spell check
1 parent 973241b commit e4354c2

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

spellcheck.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python3
22

33
import argparse
4+
5+
import concurrent.futures
6+
47
import os
58
import re
69
import subprocess
@@ -98,21 +101,35 @@ def main():
98101
num_files_with_errors = 0
99102
num_files_with_processing_errors = 0
100103

101-
for notebook in args.notebooks:
102-
num_files_processed += 1
103-
error_output = check_spelling(notebook)
104-
if error_output:
105-
all_errors.append(error_output)
106-
if (
107-
"Error running command" in error_output
108-
or "An unexpected error occurred" in error_output
109-
or "Error - File not found" in error_output
110-
):
104+
futures = []
105+
with concurrent.futures.ProcessPoolExecutor() as executor:
106+
for notebook in args.notebooks:
107+
futures.append(executor.submit(check_spelling, notebook))
108+
109+
for future in concurrent.futures.as_completed(futures):
110+
num_files_processed += 1
111+
try:
112+
error_output = future.result()
113+
if error_output:
114+
all_errors.append(error_output)
115+
if (
116+
"Error running command" in error_output
117+
or "An unexpected error occurred" in error_output
118+
or "Error - File not found" in error_output
119+
):
120+
num_files_with_processing_errors += 1
121+
else:
122+
num_files_with_errors += 1
123+
except Exception as exc:
124+
print(f"An unexpected error occurred processing a task: {exc}", file=sys.stderr)
111125
num_files_with_processing_errors += 1
112-
else:
113-
num_files_with_errors += 1
126+
all_errors.append(
127+
f"**Unknown File**: An unexpected error occurred during processing:\n```\n{exc}\n```"
128+
)
114129

115130
if all_errors:
131+
all_errors.sort()
132+
116133
print("## Spell Check Report\n")
117134
print("\n\n---\n\n".join(all_errors))
118135

@@ -123,13 +140,15 @@ def main():
123140
summary_lines.append(
124141
f"Encountered processing errors in {num_files_with_processing_errors} file(s)."
125142
)
126-
if not summary_lines:
143+
if not summary_lines and all_errors:
127144
summary_lines.append(f"Found issues in {len(all_errors)} file(s).")
128145

129-
print(f"\n---\nChecked {num_files_processed} notebook(s). " + " ".join(summary_lines))
146+
total_notebooks_input = len(args.notebooks)
147+
print(f"\n---\nChecked {total_notebooks_input} notebook(s). " + " ".join(summary_lines))
130148
sys.exit(1)
131149
else:
132-
print(f"Spell check passed successfully for {num_files_processed} notebook(s).")
150+
total_notebooks_input = len(args.notebooks)
151+
print(f"Spell check passed successfully for {total_notebooks_input} notebook(s).")
133152
sys.exit(0)
134153

135154

0 commit comments

Comments
 (0)