Skip to content

Commit 4faaaed

Browse files
authored
Merge pull request #33 from jcfr/prefer-f-strings
ENH: Simplify reporting using f-string
2 parents ce1372e + 6195e02 commit 4faaaed

1 file changed

Lines changed: 20 additions & 27 deletions

File tree

codespell.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
8888
mime_type = getMimeType(filename)
8989

9090
if output_lvl > 0:
91-
print("spell_check_file:", filename, ",", mime_type)
91+
print(f"spell_check_file: {filename}, {mime_type}")
9292

9393
# Returns a list of comment_parser.parsers.common.Comments
9494
if mime_type == "text/plain":
@@ -97,22 +97,22 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
9797
try:
9898
clist = comment_parser.extract_comments(filename, mime=mime_type)
9999
except BaseException:
100-
print("Parser failed, skipping file", filename)
100+
print(f"Parser failed, skipping file {filename}")
101101
return []
102102

103103
bad_words = []
104104

105105
for c in clist:
106106
if output_lvl > 1:
107-
print("Comment: ", c)
107+
print(f"Comment: {c}")
108108
print(type(c))
109109

110110
mistakes = []
111111
spell_checker.set_text(c.text())
112112

113113
for error in spell_checker:
114114
if output_lvl > 1:
115-
print("Error:", error.word)
115+
print(f"Error: {error.word}")
116116

117117
# Check if the bad word starts with a prefix.
118118
# If so, spell check the word without that prefix.
@@ -126,17 +126,17 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
126126
# remove the prefix
127127
wrd = error.word[len(pre) :]
128128
if output_lvl > 1:
129-
print("Trying without prefix: ", error.word, wrd)
129+
print(f"Trying without '{pre}' prefix: {error.word} -> {wrd}")
130130
try:
131131
if spell_checker.check(wrd):
132132
continue
133133
except BaseException:
134-
print("Caught an exception for word", error.word, wrd)
134+
print(f"Caught an exception for word {error.word} {wrd}")
135135

136136
# Try splitting camel case words and checking each sub-word
137137

138138
if output_lvl > 1:
139-
print("Trying splitting camel case word")
139+
print(f"Trying splitting camel case word: {error.word}")
140140
sub_words = splitCamelCase(error.word)
141141
if len(sub_words) > 1:
142142
ok_flag = True
@@ -154,26 +154,19 @@ def spell_check_file(filename, spell_checker, mime_type="", output_lvl=1, prefix
154154
continue
155155

156156
if output_lvl > 1:
157-
msg = (
158-
"error: "
159-
+ "'"
160-
+ error.word
161-
+ "', "
162-
+ "suggestions: "
163-
+ str(spell_checker.suggest())
164-
)
157+
msg = f"error: '{error.word}', suggestions: {spell_checker.suggest()}"
165158
else:
166159
msg = error.word
167160
mistakes.append(msg)
168161

169162
if len(mistakes):
170163
if output_lvl > 0:
171-
print("\nLine number", c.line_number())
164+
print(f"\nLine number {c.line_number()}")
172165
if output_lvl > 0:
173166
print(c.text())
174167
for m in mistakes:
175168
if output_lvl >= 0:
176-
print(" ", m)
169+
print(f" {m}")
177170
bad_words.append([m, filename, c.line_number()])
178171

179172
bad_words = sorted(bad_words)
@@ -383,15 +376,15 @@ def main():
383376
suffixes = args.suffix
384377

385378
if output_lvl > 1:
386-
print("Prefixes:", prefixes)
387-
print("Suffixes:", suffixes)
379+
print(f"Prefixes: {prefixes}")
380+
print(f"Suffixes: {suffixes}")
388381

389382
#
390383
# Spell check the files
391384
#
392385
for f in file_list:
393386
if not args.miss:
394-
print("\nChecking", f)
387+
print(f"\nChecking {f}")
395388

396389
# If f is a directory, recursively check for files in it.
397390
if os.path.isdir(f):
@@ -407,11 +400,11 @@ def main():
407400
for x in dir_entries:
408401
if exclude_check(x, args.exclude) or skip_check(x, args.skip):
409402
if not args.miss:
410-
print("\nExcluding", x)
403+
print(f"\nExcluding {x}")
411404
continue
412405

413406
if not args.miss:
414-
print("\nChecking", x)
407+
print(f"\nChecking {x}")
415408
result = spell_check_file(
416409
x,
417410
spell_checker,
@@ -425,7 +418,7 @@ def main():
425418
# f is a file
426419
if exclude_check(f, args.exclude) or skip_check(f, args.skip):
427420
if not args.miss:
428-
print("\nExcluding", x)
421+
print(f"\nExcluding {x}")
429422
continue
430423

431424
# f is a file, so spell check it
@@ -448,21 +441,21 @@ def main():
448441

449442
for misspelled_word, found_file, line_num in bad_words:
450443
if misspelled_word != previous_word:
451-
print("\n", misspelled_word, ":", sep="")
444+
print(f"\n{misspelled_word}:")
452445

453446
if (misspelled_word == previous_word) and args.first:
454447
sys.stderr.write(".")
455448
continue
456449

457450
if args.vim:
458-
print(" vim +", line_num, " ", found_file, sep="", file=sys.stderr)
451+
print(f" vim +{line_num} {found_file}", file=sys.stderr)
459452
else:
460-
print(" ", found_file, ", ", line_num, sep="", file=sys.stderr)
453+
print(f" {found_file}, {line_num}", file=sys.stderr)
461454

462455
previous_word = misspelled_word
463456

464457
print("")
465-
print(len(bad_words), "misspellings found")
458+
print(f"{len(bad_words)} misspellings found")
466459

467460
sys.exit(len(bad_words))
468461

0 commit comments

Comments
 (0)