Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| python:accueil [2021/12/28 18:53] – [Yaml] fix loading function phsw | python:accueil [2025/09/04 09:32] (Version actuelle) – [Chaînes de caractères] add link phsw | ||
|---|---|---|---|
| Ligne 17: | Ligne 17: | ||
| * [[https:// | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| + | * [[https:// | ||
| * [[https:// | * [[https:// | ||
| Ligne 92: | Ligne 93: | ||
| ==== argparse ==== | ==== argparse ==== | ||
| + | |||
| + | * https:// | ||
| Permet de gérer facilement les arguments passés à un script, et générer la documentation qui va avec. | Permet de gérer facilement les arguments passés à un script, et générer la documentation qui va avec. | ||
| Ligne 100: | Ligne 103: | ||
| cli_parser.add_argument(" | cli_parser.add_argument(" | ||
| cli_parser.add_argument(" | cli_parser.add_argument(" | ||
| - | args = cli_parser.parse_args(); | + | args = cli_parser.parse_args() |
| </ | </ | ||
| '' | '' | ||
| + | |||
| + | Pour avoir un paramètre optionnel avec une valeur par défaut : | ||
| + | <code python> | ||
| + | cli_parser.add_argument(" | ||
| + | </ | ||
| + | |||
| Ligne 170: | Ligne 179: | ||
| writer.writerows(someiterable) | writer.writerows(someiterable) | ||
| </ | </ | ||
| + | |||
| + | Il est possible d' | ||
| Ligne 178: | Ligne 189: | ||
| next(reader) | next(reader) | ||
| </ | </ | ||
| + | |||
| + | |||
| + | === Lire l' | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <code python> | ||
| + | import sys | ||
| + | |||
| + | |||
| + | f = open(sys.argv[1]) if len(sys.argv) == 2 else sys.stdin | ||
| + | |||
| + | # ... work with f ... | ||
| + | |||
| + | if len(sys.argv) == 2: | ||
| + | f.close() | ||
| + | </ | ||
| + | |||
| ==== Ecrire sur stderr ==== | ==== Ecrire sur stderr ==== | ||
| + | |||
| + | * [[https:// | ||
| <code python> | <code python> | ||
| Ligne 271: | Ligne 302: | ||
| === Formatage de chaînes de caractères === | === Formatage de chaînes de caractères === | ||
| - | [[https:// | + | [[https:// |
| Il faut doubler les accolades pour qu' | Il faut doubler les accolades pour qu' | ||
| Ligne 351: | Ligne 382: | ||
| </ | </ | ||
| + | |||
| + | |||
| + | === Trouver le préfixe commun === | ||
| + | |||
| + | * [[https:// | ||
| + | * https:// | ||
| + | |||
| + | <code python> | ||
| + | import os | ||
| + | |||
| + | os.path.commonprefix([str1, | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Connaître la langue d'un texte === | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | L' | ||
| + | <code bash> | ||
| + | pip install numpy six icu | ||
| + | pip install pyicu | ||
| + | pip install pycld2 | ||
| + | pip install morfessor | ||
| + | </ | ||
| + | <code python> | ||
| + | from polyglot.text import Text | ||
| + | if Text(txt).language.code not in [" | ||
| + | printf(" | ||
| + | </ | ||
| Ligne 418: | Ligne 479: | ||
| + | === Mesurer des durées === | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | <code python> | ||
| + | from timeit import default_timer as timer | ||
| + | |||
| + | start = timer() | ||
| + | # ... | ||
| + | end = timer() | ||
| + | print(end - start) # Time in seconds, e.g. 5.38091952400282 | ||
| + | </ | ||
| Ligne 448: | Ligne 521: | ||
| </ | </ | ||
| + | Trouver toutes les occurrences qui correspondent à un motif ([[https:// | ||
| + | <code python> | ||
| + | re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats') | ||
| + | # Output: [' | ||
| + | </ | ||
| + | === Remplacer un motif === | ||
| + | |||
| + | <code python> | ||
| + | re.sub(r' | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ==== Flottants ==== | ||
| + | |||
| + | === Tronquer un flottant === | ||
| + | |||
| + | Utiliser la fonction '' | ||
| + | <code python> | ||
| + | round(1.23456, | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Tester si une chaîne est un flottant === | ||
| + | |||
| + | <code python> | ||
| + | s.replace(' | ||
| + | </ | ||
| + | |||
| + | |||
| + | === Tester si un flottant est NaN === | ||
| + | |||
| + | <code python> | ||
| + | import math | ||
| + | |||
| + | math.isnan(f) | ||
| + | </ | ||
| Ligne 535: | Ligne 647: | ||
| <code python> | <code python> | ||
| bin(14) # ' | bin(14) # ' | ||
| - | </ | ||
| - | |||
| - | |||
| - | === Tronquer un flottant === | ||
| - | |||
| - | Utiliser la fonction '' | ||
| - | <code python> | ||
| - | round(1.23456, | ||
| - | </ | ||
| - | |||
| - | |||
| - | === Tester si une chaîne est un flottant === | ||
| - | |||
| - | <code bash> | ||
| - | s.replace(' | ||
| </ | </ | ||
| Ligne 569: | Ligne 666: | ||
| import os | import os | ||
| redirected_to_file = not (os.fstat(0) == os.fstat(1)) | redirected_to_file = not (os.fstat(0) == os.fstat(1)) | ||
| + | </ | ||
| + | |||
| + | Une solution plus simple (et semble plus fonctionnelle), | ||
| + | <code python> | ||
| + | import sys | ||
| + | |||
| + | if sys.stdout.isatty(): | ||
| + | print "Not redirected" | ||
| + | else: | ||
| + | sys.stderr.write(" | ||
| </ | </ | ||
| Ligne 599: | Ligne 706: | ||
| + | === Connaître le nom du script exécuté === | ||
| + | [[https:// | ||
| - | + | <code python> | |
| + | import os | ||
| + | os.path.basename(__file__) | ||
| + | </ | ||
| ==== Dictionnaires ==== | ==== Dictionnaires ==== | ||
| Ligne 695: | Ligne 806: | ||
| + | ==== Débugguer un segfault ==== | ||
| + | * [[https:// | ||
| + | * https:// | ||
| + | |||
| + | <code bash> | ||
| + | export PYTHONFAULTHANDLER=1 | ||
| + | </ | ||