python:accueil

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
python:accueil [2024/01/28 20:23] – [Divers] move float stuff to its own section and add math.isnan phswpython:accueil [2025/09/04 09:32] (Version actuelle) – [Chaînes de caractères] add link phsw
Ligne 17: Ligne 17:
   * [[https://gist.github.com/sloria/7001839|The Best of the Best Practices (BOBP) Guide for Python]]   * [[https://gist.github.com/sloria/7001839|The Best of the Best Practices (BOBP) Guide for Python]]
   * [[https://www.thecodeship.com/patterns/guide-to-python-function-decorators/|A guide to Python's function decorators]]   * [[https://www.thecodeship.com/patterns/guide-to-python-function-decorators/|A guide to Python's function decorators]]
 +  * [[https://www.nicholashairs.com/posts/major-changes-between-python-versions/|Summary of Major Changes Between Python Versions]]
  
   * [[https://stackoverflow.com/questions/31375656/how-to-draw-the-paths-of-a-networkx-graph-using-different-colours]]   * [[https://stackoverflow.com/questions/31375656/how-to-draw-the-paths-of-a-networkx-graph-using-different-colours]]
Ligne 188: Ligne 189:
 next(reader) next(reader)
 </code> </code>
 +
 +
 +=== Lire l'entrée standard ou un fichier ===
 +
 +[[https://stackoverflow.com/questions/1450393/how-do-i-read-from-stdin|Source]]
 +
 +<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()
 +</code>
 +
  
  
  
 ==== Ecrire sur stderr ==== ==== Ecrire sur stderr ====
 +
 +  * [[https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/|Redirecting all kinds of stdout in Python]]
  
 <code python> <code python>
Ligne 281: Ligne 302:
 === Formatage de chaînes de caractères === === Formatage de chaînes de caractères ===
  
-[[https://pyformat.info/]]+[[https://pyformat.info/]], [[https://fstring.help/cheat/]]
  
 Il faut doubler les accolades pour qu'elles soient ignorées par Python: Il faut doubler les accolades pour qu'elles soient ignorées par Python:
Ligne 374: Ligne 395:
 </code> </code>
  
 +
 +=== Connaître la langue d'un texte ===
 +
 +  * [[https://stackoverflow.com/questions/39142778/how-to-determine-the-language-of-a-piece-of-text/47106810]]
 +
 +L'installation de Polyglot est un peu particulière ([[https://stackoverflow.com/questions/64886067/polyglot-importerror-cannot-import-name-locale-from-icu|source]]) :
 +<code bash>
 +pip install numpy six icu
 +pip install pyicu
 +pip install pycld2
 +pip install morfessor
 +</code>
 +<code python>
 +from polyglot.text import Text
 +if Text(txt).language.code not in ["en", "fr"]:
 +    printf("...")
 +</code>
  
  
Ligne 481: Ligne 519:
     day = int(m.group(1))     day = int(m.group(1))
     month = int(m.group(2))     month = int(m.group(2))
 +</code>
 +
 +Trouver toutes les occurrences qui correspondent à un motif ([[https://stackoverflow.com/questions/4697882/how-can-i-find-all-matches-to-a-regular-expression-in-python|source]]) :
 +<code python>
 +re.findall( r'all (.*?) are', 'all cats are smarter than dogs, all dogs are dumber than cats')
 +# Output: ['cats', 'dogs']
 </code> </code>
  
Ligne 511: Ligne 555:
  
  
-==== Tester si un flottant est NaN ====+=== Tester si un flottant est NaN ===
  
 <code python> <code python>
Ligne 762: Ligne 806:
  
  
 +==== Débugguer un segfault ====
  
 +  * [[https://stackoverflow.com/questions/16731115/how-to-debug-a-python-segmentation-fault]]
 +  * https://docs.python.org/3/library/faulthandler.html
 +
 +<code bash>
 +export PYTHONFAULTHANDLER=1
 +</code>
  
  • python/accueil.1706469823.txt.gz
  • Dernière modification : 2024/01/28 20:23
  • de phsw