Conventions et PEP 8

Principes d’ingénierie logicielle en Python

Adam Spannbauer

Machine Learning Engineer at Eastman

Qu'est-ce qu'une convention ?

Poing contre poing

Personne s'inclinant

Poignée de main

Logo Python

Principes d’ingénierie logicielle en Python

Introduction à PEP 8

PEP 8

"Le code est lu bien plus souvent qu'il n'est écrit"

Principes d’ingénierie logicielle en Python

Violation de PEP 8

#define our data
my_dict ={
    'a'  : 10,
'b': 3,
    'c'  :   4,
             'd': 7}
#import needed package
import numpy as np
#helper function
def DictToArray(d):
    """Convert dictionary values to numpy array"""
    #extract values and convert
              x=np.array(d.values())
              return x
print(DictToArray(my_dict))
array([10,  4,  3,  7])
Principes d’ingénierie logicielle en Python

Suivre PEP 8

# Importer le package nécessaire
import numpy as np

# Définir nos données
my_dict = {'a': 10, 'b': 3, 'c': 4, 'd': 7}


# Fonction d'aide
def dict_to_array(d):
    """Convertir les valeurs du dictionnaire en tableau numpy"""
    # Extraire les valeurs et convertir
    x = np.array(d.values())
    return x


print(dict_to_array(my_dict))
array([10,  4,  3,  7])
Principes d’ingénierie logicielle en Python

Outils PEP 8

PEP 8 avec PyCharm

package pycodestyle

Principes d’ingénierie logicielle en Python

Utiliser pycodestyle

datacamp@server:~$ pip install pycodestyle
datacamp@server:~$ pycodestyle dict_to_array.py
dict_to_array.py:5:9: E203 espace avant ':'
dict_to_array.py:6:14: E131 ligne de continuation non alignée pour l'indentation suspendue
dict_to_array.py:8:1: E265 le commentaire de bloc doit commencer par '# '
dict_to_array.py:9:1: E402 importation de module au niveau supérieur non en haut du fichier
dict_to_array.py:11:1: E302 2 lignes vides attendues, 0 trouvées
dict_to_array.py:13:15: E111 l'indentation n'est pas un multiple de quatre
Principes d’ingénierie logicielle en Python

Sortie de pycodestyle

sortie d'erreur pycodestyle

Principes d’ingénierie logicielle en Python

Passons à la pratique !

Principes d’ingénierie logicielle en Python

Preparing Video For Download...