In [1]:
import logging
import functools
import itertools
import numpy as np
logging.root.setLevel(logging.DEBUG)
In [2]:
# make sure you have the pep8_magic installed
# jupyter nbextension install --user pep8_magic.py
%load_ext pep8_magic
In [3]:
%%pep8
a=1
In [ ]:
%%pep8
# BAD
if True:
n_spaces = 3
In [ ]:
%%pep8
# GOOD
if True:
n_spaces = 4
In [ ]:
%%pep8
# BAD
# Aligned with 4 spaces
foo = function(var_one, var_two,
var_three, var_four)
In [ ]:
%%pep8
# GOOD
# Aligned with opening parenthesis
foo = function(var_one, var_two,
var_three, var_four)
In [ ]:
%%pep8
# BAD
# Longer than 79 chars
# possible division error
import math
minkowski_distance_3d_from_origin = lambda x, y, z, p: math.pow(math.pow(x, p) + math.pow(x, p) + math.pow(x, p), 1/p)
In [ ]:
%%pep8
# GOOD
# Shorter than 79
# Use scipy, numpy
import scipy.spatial
import numpy as np
def minkowski_distance_3d_from_origin(x, y, z, p):
"""Compute the minkowski distance for a point
in 3d space from the origin"""
u = np.array([x, y, z])
v = np.zeros_like(u)
distance = scipy.spatial.distance.minkowski(u, v, p)
In [ ]:
%%pep8
# BAD
import numpy as np
class Calculator:
def Plus(self, x, y):
return np.sum(x, y)
def Minus(self, x, y):
return np.sum(x, -y)
In [ ]:
%%pep8
# GOOD
import numpy as np
class Calculator:
def add(self, x, y):
return x + y
def subtract(self, x, y):
return x - y
In [ ]:
%%pep8
# BAD
import numpy, scipy, io, os
In [ ]:
%%pep8
# GOOD
# python packages first
import os
import io
# extra packages
import numpy
import scipy
# import own packages
In [ ]:
%%pep8
# GOOD
from ctypes import (c_void_p, c_int,
c_double, c_char_p,
cdll, windll)
In [ ]:
%%pep8
# BAD
a=x+1
a = x + 1
b = a
name = c
In [ ]:
%%pep8
# GOOD
a = x + 1
a = x + 1
b = a
name = c
In [ ]:
%%pep8
# BAD
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
In [ ]:
%%pep8
# GOOD
def complex(real, imag=0.0):
return magic(r=real, i=imag)
In [ ]:
%%pep8
# BAD
a, b, c = 0, 0, 0
a += 1; b += 2; c += 3
In [ ]:
%%pep8
# GOOD
a = b = c = 0
a += 1
b += 2
c += 3
In [ ]:
%%pep8
# BAD
def jarekenmaar():
# This comment is in Dutch
# deze functie rekent, je weet toch
# dus reken maar uit.
1 + 1
In [ ]:
%%pep8
# GOOD
# English only
def calculate():
# do an essential calculation
1 + 1