#!/usr/bin/env python # coding: utf-8 # # 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 get_ipython().run_line_magic('load_ext', 'pep8_magic') # In[3]: get_ipython().run_cell_magic('pep8', '', 'a=1\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\nif True:\n n_spaces = 3\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\nif True:\n n_spaces = 4\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\n# Aligned with 4 spaces\nfoo = function(var_one, var_two,\n var_three, var_four)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\n# Aligned with opening parenthesis\nfoo = function(var_one, var_two,\n var_three, var_four)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\n# Longer than 79 chars\n# possible division error\nimport math\n\nminkowski_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)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\n# Shorter than 79\n# Use scipy, numpy\nimport scipy.spatial\nimport numpy as np\n\n\ndef minkowski_distance_3d_from_origin(x, y, z, p):\n """Compute the minkowski distance for a point\n in 3d space from the origin"""\n u = np.array([x, y, z])\n v = np.zeros_like(u)\n distance = scipy.spatial.distance.minkowski(u, v, p)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\nimport numpy as np\nclass Calculator:\n def Plus(self, x, y):\n return np.sum(x, y)\n def Minus(self, x, y):\n return np.sum(x, -y)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\nimport numpy as np\n\n\nclass Calculator:\n def add(self, x, y):\n return x + y\n\n def subtract(self, x, y):\n return x - y\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\nimport numpy, scipy, io, os\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\n# python packages first\nimport os\nimport io\n\n# extra packages\nimport numpy\nimport scipy\n\n# import own packages\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\nfrom ctypes import (c_void_p, c_int,\n c_double, c_char_p,\n cdll, windll)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\na=x+1\n a = x + 1\n b = a\nname = c\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\na = x + 1\na = x + 1\nb = a\nname = c\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\ndef complex(real, imag = 0.0):\n return magic(r = real, i = imag)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\ndef complex(real, imag=0.0):\n return magic(r=real, i=imag)\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\na, b, c = 0, 0, 0\na += 1; b += 2; c += 3\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\na = b = c = 0\na += 1\nb += 2\nc += 3\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# BAD\ndef jarekenmaar():\n # This comment is in Dutch\n # deze functie rekent, je weet toch\n # dus reken maar uit.\n 1 + 1\n') # In[ ]: get_ipython().run_cell_magic('pep8', '', '# GOOD\n# English only\ndef calculate():\n # do an essential calculation\n 1 + 1\n \n')