Skip to content

Instantly share code, notes, and snippets.

@astrojuanlu
Last active April 27, 2017 11:00
Show Gist options
  • Save astrojuanlu/9082229 to your computer and use it in GitHub Desktop.
Save astrojuanlu/9082229 to your computer and use it in GitHub Desktop.
IPython cell magic to check for PEP8 using https://pypi.python.org/pypi/pep8
# IPython magic to check for PEP8 compliance.
# Author: Juan Luis Cano <juanlu001@gmail.com>
"""IPython magic to check for PEP8 compliance.
To use it, type
```%load_ext pep8magic```
and then
```%%pep8
if 6*9==42:print("Something fundamentally wrong..." )
```
to see PEP8 failures.
"""
import pep8 as _pep8
def pep8(line, cell):
lines = cell.splitlines(True)
lines[-1] += '\n'
fchecker = _pep8.Checker(lines=lines,
show_source=True)
report = fchecker.check_all()
if report == 0:
print("This code is PEP8-compliant!")
def load_ipython_extension(ipython):
ipython.register_magic_function(pep8, magic_kind='cell')
@astrojuanlu
Copy link
Author

To test it:

%load_ext pep8magic

And then:

%%pep8

print("This code passes!")

@wonbyte
Copy link

wonbyte commented Mar 29, 2015

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment