Automatic Style Formatting


404 image

Photos/Images: Clement H
Photos/Images by #WOCinTech/#WOCinTech Chat.

PEP8

PEP8 is a style guide that recommends formatting such as:

  • Indenting using 4 spaces
  • Having whitespace around operators, e.g. x = 1 not x=1
  • Avoiding extra whitespace such as f(1), not f (1)
  • Single and double quotes for strings, but only using “””triple double quotes”””, not ’’’triple single quotes’’’
  • Variable and function names using underscores_between_words


flake8

404 image

Black

Before black

def exponent_a_list(numerical_list   ,   exponent   =      2):

    if type(numerical_list         ) is    not list   :
                raise Exception(     "You are not using a list for the numerical_list input."   
                )

    new_exponent_list   = list()
    for   number_in_new_exponent_list in              numerical_list:
                new_exponent_list.append(                  number_in_new_exponent_list **  exponent  )
    return new_exponent_list

After black

def exponent_a_list(numerical_list, exponent=2):

    if type(numerical_list) is not list:
        raise Exception("You are not using a list for the numerical_list input.")

    new_exponent_list = list()
    for number_in_new_exponent_list in numerical_list:
        new_exponent_list.append(number_in_new_exponent_list ** exponent)
    return new_exponent_list

After black

def exponent_a_list(numerical_list, exponent=2):

    if type(numerical_list) is not list:
        raise Exception("You are not using a list for the numerical_list input.")

    new_exponent_list = list()
    for number_in_new_exponent_list in numerical_list:
        new_exponent_list.append(number_in_new_exponent_list ** exponent)
    return new_exponent_list


Checking with flake8 again

404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image


404 image

Let’s apply what we learned!