EzDevInfo.com

pep8

Simple Python style checker in one Python file

How to configure PyLint to check all things PEP8 checks?

Searching for an answer on PyLint's mailing list brings no interesting results.
PyLint is known to be very customizable so I guess this should be possible...

The reason I would like PyLint to check compliance with PEP8 is because

  • PyDev has much better support for PyLint than it has for PEP8.
  • It's easier to have one tool doing all checks than having to use two.

I also asked this question on PyLint's mailing list at http://thread.gmane.org/gmane.comp.python.logilab/1039

Example of diagnostic messages from PEP8 which I don't get from PyLint:

  • E203 whitespace before ':'
  • E225 missing whitespace around operator
  • E251 no spaces around keyword / parameter equals
  • E301 expected 1 blank line, found 0
  • E303 too many blank lines
  • E501 line too long (90 characters)
  • W291 trailing whitespace
  • W292 no newline at end of file
  • W293 blank line contains whitespace

Source: (StackOverflow)

Vim: Use shorter textwidth in comments and docstrings

From the mighty PEP 8:

[P]lease limit all lines to a maximum of 79 characters. For flowing long blocks of text (docstrings or comments), limiting the length to 72 characters is recommended.

When editing Python code in Vim, I set my textwidth to 79, and Vim automatically wraps long lines of Python code for me when I hit the character limit.

But in comments and docstrings, I need to wrap text at 72 characters instead. Is there any way to make Vim automatically set textwidth to 72 when I'm in a comment or docstring, and set it back when I'm done?


Source: (StackOverflow)

Advertisements

How to fix: W602 deprecated form of raising exception

If I use pylint (via sublimerlinter) I get following warning message:

W602 deprecated form of raising exception

This I how I use exceptions in my code:

if CONDITION == True:
    raise ValueError, HELPING_EXPLANATION

Source: (StackOverflow)

Tool for automatically check docstring style according to PEP257

Tools like pep8 can check source code style, but they don't check if docstrings are fromatted according to pep257, pep287. Are there such tools?

Update

I decided to implement such a static analysis tool on my own, see:

https://github.com/GreenSteam/pep257

Right now, most of pep257 is covered. Design was heavily influenced by mentioned pep8 tool.


Source: (StackOverflow)

PyCharm and filters for external tools

I'm trying out PyCharm for Django development and so far am extremely happy. My team strictly follows PEP8 formatting and we use the pep8 command line program to check to make sure our code conforms.

I've configured an external tool command to run pep8 and it works good. I see the capability to create filters that will cause the output to be parsed into something PyCharm can use. I've read the docs and searched Google but can't find an example to make this work. Docs are http://www.jetbrains.com/pycharm/webhelp/add-filter-dialog.html

I'm using PyCharm 1.2 and the output filter I'm using looks like this:

$FILE_PATH$:$LINE$:$COLUMN:.*

Example output looks like this:

/home/matt/.../settings.py:13:30: E261 at least two spaces before inline comment
/home/matt/.../settings.py:20:80: E501 line too long (126 characters)

What would be even more awesome is if this could be run each time the file is saved.


Source: (StackOverflow)

What is the naming convention for Python class references

What is the naming convention for a variable referencing a class in Python?

class MyClass(object):
    pass

# which one is correct?
reference_to_class = MyClass

# or
ReferenceToClass = MyClass

Here is another example that resembles my situation:

# cars.py
class Car(object):
    pass

class Sedan(Car):
    pass

class Coupe(Car):
    pass

class StatonWagon(Car):
    pass

class Van(Car):
    pass

def get_car_class(slug, config):
    return config.get(slug)

# config.py
CONFIG = {
    'ford-mustang': Coupe,
    'buick-riviera': Coupe,
    'chevrolet-caprice': Sedan,
    'chevy-wan' Van:
    'ford-econoline': Van
}

# main.py
from config.py import CONFIG
from cars import get_car_class

MyCarClass = get_car_class('buick-riviera')

my_car = MyCarClass()

I would prefer ReferenceToClass, that everybody new to the code knows it's a class and not an instance. But as @poplitea wrote, literature reference would be great.


Source: (StackOverflow)

For what reason do we have the lower_case_with_underscores naming convention? [closed]

Depending on your interpretation this may or may not be a rhetorical question, but it really baffles me. What sense does this convention make? I understand naming conventions don't necessarily have to have a rhyme or reason behind them, but why deviate from the already popular camelCase? Is there a rhyme and reason behind lower_case_with_underscores that I'm somehow missing? (and yes, I have read PEP 8 in its entirety, and yes, I do understand that it's merely a proposal, a guide, etc.)

I suppose my real question would be this: I'm writing a Python library. In fact, with any luck, it may be a rather large library, relative to my other projects. I already try to adhere to PEP 8 as much as possible, and so far I've even maintained lower_case_with_underscores as PEP 8 instructs for function and method names. But it bugs me that I have to remember to use camelCase for Twisted, camelCase for logging, and just about everything else. What naming convention should I use, and why?

It would probably amaze people that I care this much about naming, enough to write a lengthy question about it, and it amazes me, too. Perhaps I have a little OCD when it comes to these things. I don't have much of a "personal opinion" about it as much as I have a tendency to just go for whatever is used the most, which in this case, would be camelCase - but it annoys me even further to find out that I'm probably breaking some eternal laws about explicit vs implicit and the zen of python written in stone or something.


Source: (StackOverflow)

Auto-import doesn't follow PEP8

Consider the following code:

from bs4 import BeautifulSoup


data = "<test>test text</test>"
soup = BeautifulSoup(data)
print(soup.find(text=re.compile(r'test$')))

It is missing an import re line and would fail with a NameError without it.

Now, I'm trying to use PyCharm's Auto-Import feature: focusing on re and hitting Alt+Enter, which opens up the following popup:

enter image description here

Now, if I choose Import 're' option, Pycharm would insert the new import line at the top of the script:

import re
from bs4 import BeautifulSoup


data = "<test>test text</test>"
soup = BeautifulSoup(data)
print(soup.find(text=re.compile(r'test$')))

Looks almost good, except that it doesn't follow PEP8 import guidelines:

Imports should be grouped in the following order:

  • standard library imports

  • related third party imports

  • local application/library specific imports

You should put a blank line between each group of imports.

In other words, there is a missing blank line between the two imports:

import re

from bs4 import BeautifulSoup

Question is: is it possible to tell Pycharm to follow the PEP8 guidelines and insert a new-line between the lines with different import types on auto-import?


As a workaround, I'm calling Optimize Imports after that organizes the imports correctly.


Source: (StackOverflow)

How to properly use python's isinstance() to check if a variable is a number?

I found some old Python code that was doing something like:

if type(var) is type(1):
   ...

As expected, pep8 complains about this recommending usage of isinstance().

Now, the problem is that the numbers module was added in Python 2.6 and I need to write code that works with Python 2.5+

So if isinstance(var, Numbers.number) is not a solution.

Which would be the proper solution in this case?


Source: (StackOverflow)

How to disable a pep8 error in a specific file?

I tried with

#:PEP8 -E223

or

# pep8: disable=E223

I thought the second would work but doesn't seems to work.

Do you have an idea how I can handle this ?

Thanks


Source: (StackOverflow)

Is there a recommended format for multi-line imports?

I have read there are three ways for coding multi-line imports in python

With slashes:

from Tkinter import Tk, Frame, Button, Entry, Canvas, Text, \
    LEFT, DISABLED, NORMAL, RIDGE, END

Duplicating senteces:

from Tkinter import Tk, Frame, Button, Entry, Canvas, Text
from Tkinter import LEFT, DISABLED, NORMAL, RIDGE, END

With parenthesis:

from Tkinter import (Tk, Frame, Button, Entry, Canvas, Text,
    LEFT, DISABLED, NORMAL, RIDGE, END)

Is there a recomended format or a more elegant way for this statements?


Source: (StackOverflow)

PyLint, PyChecker or PyFlakes? [closed]

I would like to get some feedback on these tools on :

  • features;
  • adaptability;
  • ease of use and learning curve.

Source: (StackOverflow)

What is PEP8's E128: continuation line under-indented for visual indent?

Just opened a file with Sublime Text (with Sublime Linter) and noticed a PEP8 formatting error that I'd never seen before. Here's the text:

urlpatterns = patterns('',
    url(r'^$', listing, name='investment-listing'),
)

It's flagging the second argument, the line that starts url(...)

I was about to disable this check in ST2 but I'd like to know what I'm doing wrong before I ignore it. You never know, if it seems important I might even change my ways :)


Source: (StackOverflow)

How to integrate pep8.py in Eclipse?

A little background:

  • PEP 8 is the Style Guide for Python Code. It contains the conventions all python programmers should follow.
  • pep8.py is a (very useful) script that checks the code formating of a given python script, according to PEP 8.
  • Eclipse is a great IDE. With the Pydev extension, it that can be used to develop Python

I run pep8.py manually when I'm scripting, but with bigger projects I prefer to use Eclipse. It would be really useful to integrate pep8.py in Eclipse/Pydev, so it can be run automatically in all the files in the project, and point to the lines containing the warnings. Maybe there is an obvious way to do it, but I haven't found it yet.

Question is: How to integrate pep8.py in Eclipse?


Source: (StackOverflow)

Why does Python PEP-8 specify a maximum line length of 79 characters? [closed]

Why in this millenium should Python PEP-8 specify a maximum line length of 79 characters?

Pretty much every code editor under the sun can handle longer lines. What to do with wrapping should be the choice of the content consumer, not the responsibility of the content creator.

Are there any (legitimately) good reasons for adhering to 79 characters in this age?


Source: (StackOverflow)