EzDevInfo.com

autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide. autopep8 1.2 : Python Package Index a tool that automatically formats python code to conform to the pep 8 style guide

How to prevent PyDev's autopep8 import formatter from moving site.addsitedir() calls?

The Eclipse PyDev plugin includes fantastic integrated autopep8 support. It formats the code to PEP8 style automatically on save, with several knobs and options to tailor it to your needs.

But the autopep8 import formatter breaks site.addsitedir() usage.

import site

site.addsitedir('/opt/path/lib/python')

# 'ourlib' is a package in '/opt/path/lib/python', which 
# without the above addsitedir() would otherwise not import.
from ourlib import do_stuff

And after PyDev's autopep8 import formatter, it changes it to:

import site

from ourlib import do_stuff

site.addsitedir('/opt/path/lib/python')

Which breaks from ourlib import do_stuff with ImportError: No module named ourlib.

Question:

Is there a PyDev setting or autopep8 command-line option to keep it from moving site.addsitedir() calls?


Source: (StackOverflow)

pep8, autopep8 and imports at the end of file

I'm using Eclipse with plugged autopep8 and I found it it very helpful. It's saving a lot of my time from fixing code style by hands. But for some coding patterns I don't know how to avoid pep8 rules I don't want to use. For example using Django (1.5.4) I need to connect signals of installed application. I always use import signals at the end of models.py file. But pep8 doesn't allow to use imports at end of file. # noqa comment doesn't helps. I can not put import signals to the top of the models.py file, because in signals I use some models still not defined on that moment.

What can you suggest in this situation? May be there is more appropriate way to connect signals?


Source: (StackOverflow)

Advertisements

W602 raise ValueError - How has the message to look like?

I am very new to python and my first task is to check older code (not mine!) to convert it according to pep8.

I have the following code block and I should change raise ValueError to raise ValueError("Message"). How has the syntax of the message to look like, something like 'could not find %c in %s' % (ch,str)?

def sort_key(self, string):

    collation_elements = []

    lookup_key = [ord(ch) for ch in string]
    while lookup_key:
        value, lookup_key = self.table.find_prefix(lookup_key)
        if not value:
            # @@@
            raise ValueError, map(hex, lookup_key)
        collation_elements.extend(value)

    sort_key = []

    for level in range(4):
        if level:
            sort_key.append(0)  # level separator
        for element in collation_elements:
            ce_l = int(element[1][level], 16)
            if ce_l:
                sort_key.append(ce_l)

    return tuple(sort_key)

Source: (StackOverflow)