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 [2022/06/03 23:20] – [Regex] replace patterns 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 188: | 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 281: | 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 374: | Ligne 395: | ||
| </ | </ | ||
| + | |||
| + | === 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 441: | 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 469: | Ligne 519: | ||
| day = int(m.group(1)) | day = int(m.group(1)) | ||
| month = int(m.group(2)) | month = int(m.group(2)) | ||
| + | </ | ||
| + | |||
| + | 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: [' | ||
| </ | </ | ||
| Ligne 480: | Ligne 536: | ||
| + | |||
| + | |||
| + | ==== 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 565: | 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 599: | 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 629: | Ligne 706: | ||
| + | === Connaître le nom du script exécuté === | ||
| + | [[https:// | ||
| - | + | <code python> | |
| + | import os | ||
| + | os.path.basename(__file__) | ||
| + | </ | ||
| ==== Dictionnaires ==== | ==== Dictionnaires ==== | ||
| Ligne 725: | Ligne 806: | ||
| + | ==== Débugguer un segfault ==== | ||
| + | * [[https:// | ||
| + | * https:// | ||
| + | |||
| + | <code bash> | ||
| + | export PYTHONFAULTHANDLER=1 | ||
| + | </ | ||