Automatically PEP8 & Format Your Python Code!
or as Zen of Python says "Readability Counts".
PEP8 Style Guide:Since code is read more than it is written, it is very important to make sure that the code is readable. Python has coding convetions(PEP8 style guide) to maintain consistency in code and thereby making it more readable.
There are lot of tools (like pep8, flake8, pylint) to check if Your code is in compliance with PEP8. Today most of the IDE/text editors have plugins which check for these errors and report it on the fly. If You are writing code from scratch, You can make sure that You are following coding conventions.
However once in a while You have to read/bug-fix other people's code who don't follow these conventions(or You might even stumble across Your old piece of code). Once You are accustomed to style guide, it will be uneasy to read inconsistent code. Here is a badly formatted Python file opened in Emacs editor(with flymake & flake8 configured)
Custom Style Guide:If You don't like any of PEP8 rules, You can set Your own style guide for Your project. Most developers find limiting line length to 79 characters annoying. So they set their own line limit. You can even configure PEP8 checkers to take You own guidelines and check the code according to it.
Auto PEP8:Formatting code to PEP8 style is boring & time consuming process. So instead of manually formatting code, You can use autopep8 package which automatically formats Python code to conform to the PEP 8 style guide.
To install the package run
If You have a file foo.py as show above, You can run
which neatly formats the file like this
Emacs Integration:As emacs is extremely configurable editor, with just couple of lines of lisp, we can integrate autopep8 to emacs. Just find where autopep8 is installed; write a function which runs autopep8 on current buffer; bind a key to that function; set a hook, so that when ever You save a file, it will be automatically run the function.
This works reasonably well. But I do recommend using Elpy for Python developemnt in emacs. I have just sent a pull request to integrate autopep8 with elpy.
PEP8 formatters like autopep8, pep8ify will remove only lint errors. But they can't beautify code.
Above code remains same after pep8ifying also. But the code doesn't look good yet. You can use formatters like yapf, which will format the code even if the code is PEP8 compliant.
Above code will be formatted to
Some times this even destroys Your manual formatting. For example
will be converted to
But You can tell it to ignore some parts.
Beyond PEP8:As we have learnt to auto PEP8 code, it is the time to look beyond PEP8. There is a good talk by Raymond Hettinger on that - Beyond PEP8, Best practices for beautiful intelligible code.
Let Machines take care of PEP8 & Let Humans look Beyond PEP8.
References: Code is read much more often than it is written, so plan accordingly.
Python - PEP8 style guide.
Autopep8 - formats Python code to conform to the PEP 8.
PEP8ify - modifies python source code to conform to PEP8.
Elpy, the Emacs Lisp Python Environment.
Read more articles about Python!
Read more articles about Emacs!
I am Chillar Anand. I daydream a lot and write about the things that interest me here. You can read more about this blog here.