Skip to content

Instantly share code, notes, and snippets.

@campagnola
Created March 18, 2014 19:48
Show Gist options
  • Save campagnola/9627965 to your computer and use it in GitHub Desktop.
Save campagnola/9627965 to your computer and use it in GitHub Desktop.
Readable Python examples that fail PEP8
"""
pep8 violations:
E121,E123,E126,E201,E202,E203,E221,E222,E226,E227,E228,E231,E241,E251,
E302,E303,E401,E701,W291,W293,W391
Proposed style guidelines:
1. Is this code sufficiently readable?
2. Does the formatting cause extra effort for other people?
-> indenting with tabs, DOS newlines
3. Does the style conform to all PEP8 rules not listed above?
"""
import os, sys, re
import numpy as np
x = sys
x = os
x = re
img = np.array([[ (0,0,1), (0,1,0), (0,1,0), (0,1,0), (0,1,0) ],
[ (0,1,0), (1,0,0), (1,0,0), (1,0,0), (1,0,0) ],
[ (0,1,1), (1,1,1), (1,1,1), (1,1,1), (1,1,1) ],
[ (1,0,0), (1,1,0), (1,1,0), (1,1,0), (1,1,0) ],
[ (1,1,1), (1,0,1), (1,0,1), (1,0,1), (1,0,1) ]], dtype=bool)
C1 = 0b00101001010110
C2 = 0b00101001010110
C11 = 0b00001111010110
C12 = 0b00101001111110
C21 = 0b01101001110110
C22 = 0b00100110010110
C101 = 0b00101000000110
C102 = 0b00010100101110
complex_calc = ( ( (x+1) * (x+2)**0.5 * (x+3)**2.1) /
((x>>1) * (x%2)**2 * (x|10)**0.5) )
# ---------- Beginning of a new section ----------------
# Comment tests
for i in range(10):
x.call_something(i+1)
#call_something(i) # disabled: very good reason
# Blank line with whitespace:
x.call_something_else(i)
class X:
class Y:
def fn(self):
for i in range(3):
for j in range(4):
x.call_some_very_long_function_name(
arg1="just to get close to C80",
arg2="just to get close to C80")
# Line spacing
class Z:
def public1(self):
pass
def public2(self):
pass
def public3(self):
pass
# ----- Begin private methods -------
def _private1(self):
pass
def _private2(self):
pass
def _private3(self):
pass
# Compact definitions
def x1(): pass
def x2(): pass
def x3(): pass
def x4(): pass
def x5(): pass
def x6(): pass
dict(
arg1 = (1,2,4),
long_arg = (1,2,4),
longer_arg = (1,2,4),
really_long_arg = (1,2,4),
another_arg = (1,2,4),
)
x = {
'arg1' : (1,2,4),
'long_arg' : (1,2,4),
'longer_arg' : (1,2,4),
'really_long_arg' : (1,2,4),
'another_arg' : (1,2,4),
}
# Extra space makes breaks more visible
x = {
'asdfh: llahwf': 'fabafcvoosd: ewfsdfahfjai: wfweojhaasdfg',
'asdfhjklvnm: k;gjnaeg:ahwf': 'fabafcvoosd: ewfsfojhaasdfg',
'awfjaw9pawrhwf': 'fabafcvoosd: ewfafla: klajfawfilh ojhaasdfg',
'asdfhawhnuapranhvrjkllahwf': 'fabafcv34rq89: qrafoosdkaewfojhaasdfg',
'asdfhjkllaafgaiawefjiawfpiawfjanvhwf': 'fabafcvoafo: dkaewfojhaasdfg',
'asdfwfio fawawf: kllahwf': 'fabafcvo: dkaewfojhaafhafuf9weawfasdfg',
}
# Or perhaps this is preferred?
dict(
arg = (1,2,4),
long_arg = (1,2,4),
longer_arg = (1,2,4),
really_long_arg = (1,2,4),
another_arg = (1,2,4),
arg1 = (1,2,4),
long_arg1 = (1,2,4),
longer_arg1 = (1,2,4),
really_long_arg1 = (1,2,4),
another_arg1 = (1,2,4),
arg2 = (1,2,4),
long_arg2 = (1,2,4),
longer_arg2 = (1,2,4),
really_long_arg2 = (1,2,4),
another_arg2 = (1,2,4),
)
x = [(
11435,
345235,
24645,
34564562,
2456245,
276276,
245626,
26262,
357356,
232,
27424,
457,
563523,
)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment