Maximum number of PEP8 violations in a single line

17

6

Your task is to write a file which contains a line with many pep8 violations.

The rules:

  • We use pep8 version 1.5.7 and the default settings.
  • Calling pep8 with other command line options or using a custom rc file is not allowed.
  • Maximum line length 120 characters. You can violate E501, sure, but the line which your score is calculated on has to be <= 120 characters.
  • Your module can have other lines before or after, but only one line contributes to your score.
  • Your file can contain SyntaxErrors or any kind of garbage, it needn't import or run.

Example of scoring:

The following module thing.py has a score of 2, because it contains a line (line 1) with 2 pep8 violations.

 spam='potato'

To check a score:

~$ mktmpenv 
(tmp-ae3045bd2f629a8c)~/.virtualenvs/tmp-ae3045bd2f629a8c$ pip install pep8==1.5.7
(tmp-ae3045bd2f629a8c)~/.virtualenvs/tmp-ae3045bd2f629a8c$ echo -n "spam='potato'" > thing.py
(tmp-ae3045bd2f629a8c)~/.virtualenvs/tmp-ae3045bd2f629a8c$ pep8 thing.py 
thing.py:1:5: E225 missing whitespace around operator
thing.py:1:14: W292 no newline at end of file

wim

Posted 2014-07-15T17:22:05.897

Reputation: 585

2Is this a... language-specific challenge? Because we don't really like these. – John Dvorak – 2014-07-15T17:37:10.777

I guess it's not really language specific (because the file can contain any garbage) but obviously people familiar with python coding will have some advantage – wim – 2014-07-15T17:38:06.083

Answers

11

241

if you want the most error, just go crazy with semicolon

$ cat test.py
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

$ cat test.py | wc -m
120

$ pep8 test.py | wc -l
241

most of the error are:

test.py:1:119: E231 missing whitespace after ';'
test.py:1:119: E702 multiple statements on one line (semicolon)

with those error at the end:

test.py:1:120: E703 statement ends with a semicolon
test.py:1:121: W292 no newline at end of file

freeforall tousez

Posted 2014-07-15T17:22:05.897

Reputation: 211

@wim couldn't reply to your post so i just put it here. you were right about the advantage, i be surprise if someone figure out something else that cause more error then what i posted above – freeforall tousez – 2014-07-17T00:08:18.290

I think you are a semicolon short. wc counts the trailing newline, but we don't count that for character counts on this site. As you can see in your code quote, your last character is #119. You should get 240, 2 for each semicolon except the last one, 1 for the overly long line, and 1 for ending with a semicolon. – isaacg – 2014-07-17T02:58:22.017

@isaacg ah, you are right, i thought it was weird that it gotten less error than number of character * 2, i blame gedit for adding the invisible newline :P – freeforall tousez – 2014-07-17T03:10:19.397

haha, kind of cheap .. but effective! +1 – wim – 2014-07-17T10:13:01.040

7

123

Yes, more violations than characters!

$ curl -s http://pastebin.com/raw.php?i=RwLJfa0Q | cat
 (  =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =   =
$ curl -s http://pastebin.com/raw.php?i=RwLJfa0Q | wc -m
 120
$ curl -s http://pastebin.com/raw.php?i=RwLJfa0Q | pep8 - | wc -l
 123

The trick is that an = after a ( makes pep think you're doing a keyword in a function call (e.g. foo(bar=12)). In this context, doing [TAB]= triggers both

killpep.py:1:3: E223 tab before operator
killpep.py:1:3: E251 unexpected spaces around keyword / parameter equals

And doing =[TAB] triggers both

killpep.py:1:5: E224 tab after operator
killpep.py:1:5: E251 unexpected spaces around keyword / parameter equals

Gleefully enough, you can just chain these.

This gives a violation count of one per character. I need ( to set it up, but not providing the ) gives us:

killpep.py:2:1: E901 TokenError: EOF in multi-line statement

That's 120. No newline = 121. It managed to trigger the "line too long" error, so that's 122. Finally, using one character to start with a space (thanks eric_lagergren) gives 2 violations instead of 1:

killpep.py:1:2: E111 indentation is not a multiple of four
killpep.py:1:2: E113 unexpected indentation

Victory!

Claudiu

Posted 2014-07-15T17:22:05.897

Reputation: 3 870

Add a leading whitespace and remove the last z and you'll end up with 103... but whenever I copy this code I get 83 instead of 102. I think the spacing is getting messed up. – Eric Lagergren – 2014-07-16T02:09:41.270

@eric_lagergren: I'm using tabs instead of spaces and I guess they aren't copying properly. Will pastebin it I guess! – Claudiu – 2014-07-16T11:56:29.080