User Tools

Site Tools


python_cheat_sheet

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python_cheat_sheet [2010/06/18 18:23] ginkopython_cheat_sheet [2015/01/02 13:03] (current) ginko
Line 9: Line 9:
     * Argv: (nécessite l'import du module ''sys'')<code python>argument_1=sys.argv[1]</code> (NB: argv[0] renvoie le nom du script)     * Argv: (nécessite l'import du module ''sys'')<code python>argument_1=sys.argv[1]</code> (NB: argv[0] renvoie le nom du script)
     * Il existe aussi une implémentation de getopt (//cf//. [[http://diveintopython.adrahon.org/scripts_and_streams/command_line_arguments.html|Dive into python - CLI]])     * Il existe aussi une implémentation de getopt (//cf//. [[http://diveintopython.adrahon.org/scripts_and_streams/command_line_arguments.html|Dive into python - CLI]])
 +===== Packaging ===== 
 +  * Encapsulation pour librairie autonome <code python>if __name__ == "__main__": 
 +    main()</code>
 ===== Variables ===== ===== Variables =====
   * Listes (mutable)<code python>a=[1, 'c']</code>   * Listes (mutable)<code python>a=[1, 'c']</code>
Line 35: Line 37:
 ===== IO ===== ===== IO =====
   * Ouvrir un fichier <code python>#'r'=read, 'w'=write, 'a'=append, 'r+'=read & write   * Ouvrir un fichier <code python>#'r'=read, 'w'=write, 'a'=append, 'r+'=read & write
-fichier=open('/path/to/file','r')</code>+fichier=open('/path/to/file','r') 
 +# il faut alors fermer le fichier 
 +# une métode qui referme le fichier proprement: 
 +with open('/tmp/workfile', 'r') as f: 
 +</code>
   * ''IOError'' (erreur d'entrée sortie)   * ''IOError'' (erreur d'entrée sortie)
   * Sortie vers le shell: <code python>print 'hello'</code>   * Sortie vers le shell: <code python>print 'hello'</code>
   * Prompt au shell: <code python>foo=raw_input("The Ultimate Question of Life, the Universe, and Everything")</code>NB: il existe également input(), avec laquelle python essaie de deviner le type de la variable entrée. raw_input() se contente de mettre ça dans une string.   * Prompt au shell: <code python>foo=raw_input("The Ultimate Question of Life, the Universe, and Everything")</code>NB: il existe également input(), avec laquelle python essaie de deviner le type de la variable entrée. raw_input() se contente de mettre ça dans une string.
  
 +Pour travavailler avec les chemins de fichiers : ''os.path'' et notamment ''os.path.join''.
 ===== Structure de code ===== ===== Structure de code =====
   * Boucle ''for'' <code python>#pour i de 1 à 5   * Boucle ''for'' <code python>#pour i de 1 à 5
Line 78: Line 85:
   * Classe: <code python>class formatted_string(parent_class):</code>   * Classe: <code python>class formatted_string(parent_class):</code>
   * Fonction: <code python>def function_name(arg1,arg2=10,arg3='foo'):</code>NB: les arguments ''arg2'' et ''arg3'' sont optionnels   * Fonction: <code python>def function_name(arg1,arg2=10,arg3='foo'):</code>NB: les arguments ''arg2'' et ''arg3'' sont optionnels
 +    * Expansion des arguments dans un appel de fonction:<code python>args = (1, 2, 3)
 +function_name(*args)
 +kwargs = {'arg1': [], 'arg2': 10, 'arg3': 'foo'}
 +function_name(**kwargs)</code>
 +  * Fonction anonyme (jettable, pour usage unique):<code python>lambda arg1, arg2: arg1 + arg2</code>Cf [[http://docs.python.org/tutorial/controlflow.html#lambda-forms|doc officielle]]
   * Test: <code python>if ((test1) & (test2))|(test3):</code>   * Test: <code python>if ((test1) & (test2))|(test3):</code>
  
python_cheat_sheet.1276878236.txt.gz · Last modified: 2010/06/18 18:23 by ginko