Skip to content

Commit 03af606

Browse files
Update check_structure.py
1 parent fd2858e commit 03af606

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,48 @@
1+
#!/usr/bin/env python3
12
import pathlib
23
import sys
4+
from typing import Iterator
5+
6+
# Utilisation de constantes en majuscules pour le Clean Code
7+
VALID_PATHS = (
8+
pathlib.Path("src/main/java/com/thealgorithms/"),
9+
pathlib.Path("src/test/java/com/thealgorithms/"),
10+
)
311

412

513
def _is_java_file_properly_located(java_file: pathlib.Path) -> bool:
6-
main_parents = java_file.parent.parents
7-
return (
8-
pathlib.Path("src/main/java/com/thealgorithms/") in main_parents
9-
or pathlib.Path("src/test/java/com/thealgorithms/") in main_parents
10-
)
14+
"""Vérifie si un fichier Java est dans l'un des répertoires autorisés."""
15+
# any() est plus rapide et moderne pour valider une condition parmi plusieurs
16+
return any(valid_path in java_file.parents for valid_path in VALID_PATHS)
1117

1218

13-
def _find_misplaced_java_files() -> list[pathlib.Path]:
14-
return [
19+
def _find_misplaced_java_files() -> Iterator[pathlib.Path]:
20+
"""Recherche et retourne les fichiers Java mal placés sous forme de générateur."""
21+
# Remplacement de la liste par un générateur (Iterator) pour économiser la mémoire (approche Data)
22+
return (
1523
java_file
1624
for java_file in pathlib.Path(".").rglob("*.java")
1725
if not _is_java_file_properly_located(java_file)
18-
]
26+
)
1927

2028

21-
if __name__ == "__main__":
22-
misplaced_files = _find_misplaced_java_files()
29+
def main() -> None:
30+
"""Fonction principale du script."""
31+
# Conversion du générateur en liste uniquement au moment de l'affichage
32+
misplaced_files = list(_find_misplaced_java_files())
33+
2334
if misplaced_files:
24-
print("The following java files are not located in the correct directory:")
25-
for _ in misplaced_files:
26-
print(_)
35+
# Utilisation de f-strings modernes et écriture sur la sortie d'erreur standard (stderr)
36+
print(
37+
f"❌ Error: Found {len(misplaced_files)} Java file(s) in incorrect directories:",
38+
file=sys.stderr,
39+
)
40+
for file_path in misplaced_files:
41+
print(f" - {file_path}", file=sys.stderr)
2742
sys.exit(1)
43+
44+
print("✅ Success: All Java files are properly located.")
45+
46+
47+
if __name__ == "__main__":
48+
main()

0 commit comments

Comments
 (0)