{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Styleguide" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import logging\n", "import functools\n", "import itertools\n", "\n", "import numpy as np\n", "logging.root.setLevel(logging.DEBUG)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# make sure you have the pep8_magic installed\n", "# jupyter nbextension install --user pep8_magic.py\n", "%load_ext pep8_magic" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "ename": "TypeError", "evalue": "__init__() missing 1 required positional argument: 'options'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mget_ipython\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun_cell_magic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'pep8'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m''\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'a=1'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m~/.virtualenvs/main/lib/python3.5/site-packages/IPython/core/interactiveshell.py\u001b[0m in \u001b[0;36mrun_cell_magic\u001b[0;34m(self, magic_name, line, cell)\u001b[0m\n\u001b[1;32m 2101\u001b[0m \u001b[0mmagic_arg_s\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvar_expand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mline\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstack_depth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2102\u001b[0m \u001b[0;32mwith\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuiltin_trap\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2103\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfn\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmagic_arg_s\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcell\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2104\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2105\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/src/notebooks/pep8_magic.py\u001b[0m in \u001b[0;36mpep8\u001b[0;34m(line, cell)\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0;31m# now we can check the file by name.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;31m# we might be able to use 'stdin', have to check implementation\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 55\u001b[0;31m \u001b[0mreport\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpep8_module\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mStandardReport\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 56\u001b[0m \u001b[0mpep8style\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpep8_module\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mStyleGuide\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreporter\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mreport\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0;31m# check the filename\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: __init__() missing 1 required positional argument: 'options'" ] } ], "source": [ "%%pep8\n", "a=1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8\n", "# BAD\n", "if True:\n", " n_spaces = 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8\n", "# GOOD\n", "if True:\n", " n_spaces = 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8\n", "# BAD\n", "# Aligned with 4 spaces\n", "foo = function(var_one, var_two,\n", " var_three, var_four)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8\n", "# GOOD\n", "# Aligned with opening parenthesis\n", "foo = function(var_one, var_two,\n", " var_three, var_four)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8\n", "# BAD\n", "# Longer than 79 chars\n", "# possible division error\n", "import math\n", "\n", "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)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8\n", "# GOOD\n", "# Shorter than 79\n", "# Use scipy, numpy\n", "import scipy.spatial\n", "import numpy as np\n", "\n", "\n", "def 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)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# BAD\n", "import numpy as np\n", "class 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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# GOOD\n", "import numpy as np\n", "\n", "\n", "class Calculator:\n", " def add(self, x, y):\n", " return x + y\n", "\n", " def subtract(self, x, y):\n", " return x - y\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# BAD\n", "import numpy, scipy, io, os" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# GOOD\n", "# python packages first\n", "import os\n", "import io\n", "\n", "# extra packages\n", "import numpy\n", "import scipy\n", "\n", "# import own packages" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# GOOD\n", "from ctypes import (c_void_p, c_int,\n", " c_double, c_char_p,\n", " cdll, windll)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# BAD\n", "a=x+1\n", " a = x + 1\n", " b = a\n", "name = c" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# GOOD\n", "a = x + 1\n", "a = x + 1\n", "b = a\n", "name = c" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# BAD\n", "def complex(real, imag = 0.0):\n", " return magic(r = real, i = imag)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# GOOD\n", "def complex(real, imag=0.0):\n", " return magic(r=real, i=imag)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# BAD\n", "a, b, c = 0, 0, 0\n", "a += 1; b += 2; c += 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# GOOD\n", "a = b = c = 0\n", "a += 1\n", "b += 2\n", "c += 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# BAD\n", "def jarekenmaar():\n", " # This comment is in Dutch\n", " # deze functie rekent, je weet toch\n", " # dus reken maar uit.\n", " 1 + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [], "source": [ "%%pep8 \n", "# GOOD\n", "# English only\n", "def calculate():\n", " # do an essential calculation\n", " 1 + 1\n", " " ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 1 }