Styleguide

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
---------------------------------------------------------------------------
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'
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