import logging
import functools
import itertools
import numpy as np
logging.root.setLevel(logging.DEBUG)
# make sure you have the pep8_magic installed
# jupyter nbextension install --user pep8_magic.py
%load_ext pep8_magic
%%pep8
a=1
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-c8c9ba0123f5> in <module>() ----> 1 get_ipython().run_cell_magic('pep8', '', 'a=1') ~/.virtualenvs/main/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2101 magic_arg_s = self.var_expand(line, stack_depth) 2102 with self.builtin_trap: -> 2103 result = fn(magic_arg_s, cell) 2104 return result 2105 ~/src/notebooks/pep8_magic.py in pep8(line, cell) 53 # now we can check the file by name. 54 # we might be able to use 'stdin', have to check implementation ---> 55 report = pep8_module.StandardReport() 56 pep8style = pep8_module.StyleGuide(reporter=report) 57 # check the filename TypeError: __init__() missing 1 required positional argument: 'options'
%%pep8
# BAD
if True:
n_spaces = 3
%%pep8
# GOOD
if True:
n_spaces = 4
%%pep8
# BAD
# Aligned with 4 spaces
foo = function(var_one, var_two,
var_three, var_four)
%%pep8
# GOOD
# Aligned with opening parenthesis
foo = function(var_one, var_two,
var_three, var_four)
%%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)
%%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)
%%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)
%%pep8
# GOOD
import numpy as np
class Calculator:
def add(self, x, y):
return x + y
def subtract(self, x, y):
return x - y
%%pep8
# BAD
import numpy, scipy, io, os
%%pep8
# GOOD
# python packages first
import os
import io
# extra packages
import numpy
import scipy
# import own packages
%%pep8
# GOOD
from ctypes import (c_void_p, c_int,
c_double, c_char_p,
cdll, windll)
%%pep8
# BAD
a=x+1
a = x + 1
b = a
name = c
%%pep8
# GOOD
a = x + 1
a = x + 1
b = a
name = c
%%pep8
# BAD
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
%%pep8
# GOOD
def complex(real, imag=0.0):
return magic(r=real, i=imag)
%%pep8
# BAD
a, b, c = 0, 0, 0
a += 1; b += 2; c += 3
%%pep8
# GOOD
a = b = c = 0
a += 1
b += 2
c += 3
%%pep8
# BAD
def jarekenmaar():
# This comment is in Dutch
# deze functie rekent, je weet toch
# dus reken maar uit.
1 + 1
%%pep8
# GOOD
# English only
def calculate():
# do an essential calculation
1 + 1