Pedro Rodrigues

home github

Refactoring for PEP8

I some times get a bit self-conscious about how my code looks and spend sometime making it pretty.

So, when going through a codebase that is not PEP8 compliant I open a terminal with and run:

watch pep8 module.py

Then use that as a todo list to refactor the code into concordance. As I save the changes this is automatically update.

As most people, I use the find and replace tool to help me fix issues that occur multiple times. e.g.:

E231 missing whitespace after ','

Here are some of the regular expression I use:

Tabs for spaces

# sure you know how this goes

I tend to forget spaces between parameters when these are few and small integers. So this will match commas and colons that are not followed by a space, add the space and move on.

([,:])([^\s])
\1 \2

For readability I sometimes over do with spaces between enclosing parenthesis and brackets. This one looks for parenthesis and their cousins both opening and closing, and removes following or preceding spaces.

(?:\s)([\)\]\}])|([\(\[\{])(?:\s)
\1\2

Read or