1 $ sudo pip install pep8
2 $ sudo pip install --upgrade pep8
Si on n'a pas "pip"
1 $ sudo easy_install pep8
Si on est sous Ubuntu/Debian
1 $ sudo apt-get install pep8
1 $ pep8 optparse.py
2 optparse.py:69:11: E401 multiple imports on one line
3 optparse.py:77:1: E302 expected 2 blank lines, found 1
4 optparse.py:88:5: E301 expected 1 blank line, found 0
5 optparse.py:222:34: W602 deprecated form of raising exception
6 optparse.py:347:31: E211 whitespace before '('
7 optparse.py:357:17: E201 whitespace after '{'
8 optparse.py:472:29: E221 multiple spaces before operator
9 optparse.py:544:21: W601 .has_key() is deprecated, use 'in'
... mais :
... tu vois le rapport ?
1 class Rectangle(Blob):
2
3 def __init__(self, width, height,
4 color='black', emphasis=None, highlight=0):
5 if width == 0 and height == 0 and \
6 color == 'red' and emphasis == 'strong' or \
7 highlight > 100:
8 raise ValueError("sorry, you lose")
9 if width == 0 and height == 0 and (color == 'red' or
10 emphasis is None):
11 raise ValueError("I don't think so -- values are %s, %s" %
12 (width, height))
13 Blob.__init__(self, width, height,
14 color, emphasis, highlight)
1 #!/usr/bin/env python
2 #-*- coding: utf-8 -*-
... mais on peut utiliser Latin-1
, ou win-1252
ou... nawak.
Latin-1
utf-8
1 import sys, os
1 import sys
2 import os
1 from subprocess import Popen, PIPE
... et une ligne blanche entre les groupes d'import
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Copyright 2010 Bruno Bord
4 #
5 # Licensed under the WTFPL, Version 2
6 # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
7 # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
8 # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
10 """A very simple XMPP bot.
11
12 This bot responds to simple commands. You may want to send a "help"
13 message in order to get the list of the available commands.
14
15 """
16
17 import logging
18 import os
19
20 import xmpp
21 from xmpp.protocol import Message
22
23 from xmppbot import Bot, ConnectionError
On peut faire :
1 from maclasse import MaClasse
2 from truc.chose.taclasse import TaClasse
En cas de "clash" :
1 import maclasse
2 import truc.chose.taclasse
Pour utiliser :
1 mon_objet = maclasse.MaClasse()
2 ton_autre_objet = truc.chose.taclasse.TaClasse()
1 spam( ham[ 1 ], { eggs: 2 } )
2 dict ['key'] = list [index]
1 spam(ham[1], {eggs: 2})
2 dict['key'] = list[index]
1 if x == 4 : print x , y ; x , y = y , x
1 if x == 4: print x, y; x, y = y, x
1 x = 1
2 y = 2
3 long_variable = "quelle perte de temps!"
1 x = 1
2 y = 2
3 long_variable = "quel gain de temps"
1 i=i+1
2 submitted +=1
3 x = x*2 - 1
4 hypot2 = x*x + y*y
5 c = (a+b) * (a-b)
1 i = i + 1
2 submitted += 1
3 x = x * 2 - 1
4 hypot2 = x * x + y * y
5 c = (a + b) * (a - b)
1 def complex(real, imag = 0.0):
2 return magic(r = real, i = imag)
1 def complex(real, imag=0.0):
2 return magic(r=real, i=imag)
1 if foo == 'blah': do_blah_thing()
2 do_one(); do_two(); do_three()
1 if foo == 'blah':
2 do_blah_thing()
3 do_one()
4 do_two()
5 do_three()
Python coders from non-English speaking countries: please write your comments in English, unless you are 120% sure that the code will never be read by people who don't speak your language.
Pour les codeurs Python de pays non-anglophones : merci d'écrire vos commentaires en anglais, à moins d'être sûr à 120% que le code ne sera jamais lu par quelqu'un qui ne parle pas votre langue.
-- Guido Van Rossum
1 x = x + 1 # incrémente x
2 # mouais...
3 x = x + 1 # rajoute une marge
4 # mieux
def
1 def get_foobang(plotz=None):
2 """Return a foobang.
3
4 Optional plotz says to frobnicate the bizbaz first.
5
6 """
7 bizbaz = Bizbaz()
8 if plotz:
9 bizbaz.frobnicate()
10 return Foobang(bizbaz)
1 __version__ = "$Revision: 84354 $"
2 # $Source$
utile pour Subversion, CVS, RCS...
noms de fichiers, répertoires, respect de la casse
CorrectClassName
IncorrectClassNameError
(suffixe "Error" !)get_correct_number()
get_correct_number(self)
get_correct_number(random=False)
number = my_object.get_correct_number()
ANSWER_TO_LIFE_UNIVERSE = 42
__my_private_variable
==> "privé"_not_so_private
==> "protégé"1 x = None
2 if x:
3 print "on ne passe pas ici"
4 else:
5 print "on passera"
1 x = ""
2 if x:
3 print "on ne passe pas ici"
4 else:
5 print "on passera"
1 x = None
2 if x is None:
3 print "on passera ici"
4 else:
5 print "mais jamais ici"
Une liste vide est False
. Plus performant que len()
1 my_list = []
2
3 if len(my_list) == 0:
4 print "je suis lent"
5
6 if not my_list:
7 print "je suis rapide"
1 if greetings:
2 print "good"
3
4 if greetings == True:
5 print "not good"
6
7 if greetings is True:
8 print "argh! even worse!"
1 class MessageError(Exception):
2 pass
3
4 raise MessageError, "Ouch! Here is my message!"
deux fautes - chaîne et docstring
1 class MessageError(Exception):
2 """Base class for errors in the email package."""
3 pass
4
5 raise MessageError("Ouch! Here is my message!")
1 try:
2 import platform_specific_module
3 except ImportError:
4 platform_specific_module = None
string
endswith
ou startswith
plutôt que le découpage des chaînes
1 if foo.startswith('bar'):
2 print "good"
3
4 if foo[:3] == 'bar':
5 print "not good"
Présentation réalisée avec l'aide de Landslide, Scribes, Chromium, et Ubuntu.
Cette présentation est publiée sous contrat Creative Commons CC-BY-SA.
Table of Contents | t |
---|---|
Source Files | s |
Slide Numbers | n |
Notes | 2 |
Help | h |