python:programmes:indentation
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
python:programmes:indentation [2012/02/11 22:37] – créée root | python:programmes:indentation [2013/03/12 23:06] (Version actuelle) – root | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Réindenter un script Python ====== | ====== Réindenter un script Python ====== | ||
- | < | + | Ce script permet de réindenter un script python avec au choix les tabulations ou les espaces. |
+ | |||
+ | ./ | ||
+ | |||
+ | Exemple pour réindenter le fichier toto.py en mettant 3 espaces pour la réindentation : | ||
+ | ./ | ||
+ | |||
+ | < | ||
# | # | ||
Ligne 9: | Ligne 16: | ||
print(sys.argv[0] + \ | print(sys.argv[0] + \ | ||
""" | """ | ||
- | where type indent | + | where type_indent |
- | * tab for adding tabs | + | * tab for adding tabs (default) |
* space for adding spaces | * space for adding spaces | ||
+ | |||
+ | where count is the number of type_indent : | ||
+ | * int (1 by default) | ||
| | ||
Ex : """ | Ex : """ | ||
sys.exit(error) | sys.exit(error) | ||
- | if not os.path.isfile(sys.argv[1]): | + | |
+ | if not os.path.isfile(sys.argv[1]): | ||
+ | print(sys.argv[1] + " n'est pas un fichier valide !") | ||
+ | | ||
try: | try: | ||
Ligne 54: | Ligne 67: | ||
indent[score[num_line - 1]] = indent[score[num_line - 2]] + 1 | indent[score[num_line - 1]] = indent[score[num_line - 2]] + 1 | ||
fw.write(sep * indent[score[num_line - 1]] + line[i:]) | fw.write(sep * indent[score[num_line - 1]] + line[i:]) | ||
+ | elif score[num_line - 1] not in indent and score[num_line - 1] <= score[num_line - 2]: | ||
+ | print(" | ||
+ | sys.exit(1) | ||
else: | else: | ||
fw.write(sep * indent[score[num_line - 1]] + line[i:]) | fw.write(sep * indent[score[num_line - 1]] + line[i:]) |
python/programmes/indentation.1328999857.txt.gz · Dernière modification : 2012/02/11 22:37 de root