Skip to content

Commit d5afed9

Browse files
author
MomIsBestFriend
committed
Refactored the code to use a generator
1 parent bfad571 commit d5afed9

File tree

2 files changed

+34
-36
lines changed

2 files changed

+34
-36
lines changed

ci/code_checks.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
217217
RET=$(($RET + $?)) ; echo $MSG "DONE"
218218

219219
MSG='Check for use of not concatenated strings' ; echo $MSG
220-
python $BASE_DIR/scripts/validate_string_concatenation.py pandas
220+
$BASE_DIR/scripts/validate_string_concatenation.py pandas
221221
RET=$(($RET + $?)) ; echo $MSG "DONE"
222222

223223
MSG='Check that no file in the repo contains trailing whitespaces' ; echo $MSG

scripts/validate_string_concatenation.py

+33-35
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,75 @@
1212
... "baz"
1313
... )
1414
15+
1516
into this:
1617
1718
>>> foo = ("bar " "baz")
1819
1920
Black is not considering this as an
20-
issue (see https://github.com/psf/black/issues/1051), so we are checking
21-
it here.
21+
issue (see issue https://github.com/psf/black/issues/1051),
22+
so we are checking it here.
2223
"""
2324

2425
import os
2526
import sys
2627
import token
2728
import tokenize
29+
from typing import FrozenSet, Generator, List
2830

29-
# Can be annotated as typing.FrozenSet[str]
30-
FILE_EXTENSIONS_TO_CHECK = frozenset((".pxd", ".py", ".pyx", ".pyx.ini"))
31+
FILE_EXTENSIONS_TO_CHECK: FrozenSet[str] = frozenset(
32+
(".pxd", ".py", ".pyx", ".pyx.ini")
33+
)
3134

3235

33-
def is_concatenated(file_path):
36+
def strings_to_concatenate(file_path: str) -> Generator[str, None, None]:
3437
"""
35-
Checking if the file containing strings that needs to be concatenated.
38+
Yielding the strings that needs to be concatenated in a given file.
3639
3740
Parameters
3841
----------
3942
file_path : str
4043
File path pointing to a single file.
4144
42-
Returns
43-
-------
44-
int
45-
Status code representing if the file needs a fix.
46-
0 - All good.
47-
1 - Needs to be fixed.
45+
Yields
46+
------
47+
str
48+
Message containing info about the string that needs to be concatenated.
4849
"""
49-
need_fix = False
5050
with open(file_path, "r") as file_name:
51-
tokens = list(tokenize.generate_tokens(file_name.readline))
52-
for current_token, next_token in zip(tokens, tokens[1:]):
53-
if current_token[0] == next_token[0] == token.STRING:
54-
need_fix = True
55-
print(
56-
"{file_path}:{line_number}:\t{start} and {end}".format(
57-
file_path=file_path,
58-
line_number=current_token[2][0],
59-
start=current_token[1],
60-
end=next_token[1],
61-
)
62-
)
63-
64-
return int(need_fix)
51+
tokens: List = list(tokenize.generate_tokens(file_name.readline))
52+
53+
for current_token, next_token in zip(tokens, tokens[1:]):
54+
if current_token[0] == next_token[0] == token.STRING:
55+
line_number = current_token[2][0]
56+
start = current_token[1]
57+
end = next_token[1]
58+
yield f"{file_path}:{line_number}:\t between {start} and {end}\n"
6559

6660

6761
if __name__ == "__main__":
68-
path = sys.argv[1]
62+
path: str = sys.argv[1]
6963

7064
if not os.path.exists(path):
7165
raise ValueError("Please enter a valid path, to a file/directory.")
7266

67+
failed: bool = False
68+
7369
if os.path.isfile(path):
74-
# Means that the given path is of a single file.
75-
sys.exit(is_concatenated(path))
70+
for msg in strings_to_concatenate(path):
71+
if msg:
72+
failed = True
73+
print(msg)
7674

77-
failures = 0
78-
# Means that the given path is of a directory.
7975
for subdir, _, files in os.walk(path):
8076
for file_name in files:
8177
if any(
8278
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
8379
):
8480
file_extension = os.path.join(subdir, file_name)
85-
failures += is_concatenated(os.path.join(subdir, file_name))
8681

87-
exit_code = 1 if failures >= 1 else 0
88-
sys.exit(exit_code)
82+
for msg in strings_to_concatenate(os.path.join(subdir, file_name)):
83+
if msg:
84+
failed = True
85+
print(msg)
86+
sys.exit(failed)

0 commit comments

Comments
 (0)