# eric5.UtilitiesPython2.pep8

### Global Attributes

BENCHMARK_KEYS
BINARY_OPERATORS
DEFAULT_EXCLUDE
DEFAULT_IGNORE
DOCSTRING_REGEX
E225NOT_KEYWORDS
ERRORCODE_REGEX
EXTRANEOUS_WHITESPACE_REGEX
INDENT_REGEX
MAX_LINE_LENGTH
OPERATORS
RAISE_COMMA_REGEX
SELFTEST_REGEX
SKIP_TOKENS
UNARY_OPERATORS
WHITESPACE
WHITESPACE_AROUND_NAMED_PARAMETER_REGEX
WHITESPACE_AROUND_OPERATOR_REGEX
__version__
args
options

### Classes

Checker | Load a Python source file, tokenize it, check coding style.

### Functions

_main | Parse options and run checks on Python source.
blank_lines |
compound_statements |
excluded | Check if options.exclude contains a pattern that matches filename.
expand_indent | Return the amount of indentation.
extraneous_whitespace | Avoid extraneous whitespace in the following situations:
filename_match | Check if options.filename contains a pattern that matches filename.
find_checks | Find all globally visible functions where the first argument name starts with argument_name.
get_count | Return the total count of errors and warnings.
get_error_statistics | Get error statistics.
get_statistics | Get statistics for message codes that start with the prefix.
get_warning_statistics | Get warning statistics.
ignore_code | Check if options.ignore contains a prefix of the error code.
imports_on_separate_lines |
indentation |
input_dir | Check all Python source files in this directory and all subdirectories.
input_file | Run all checks on a Python source file.
maximum_line_length | Limit all lines to a maximum of 79 characters.
message | Print a message.
missing_newline | JCR: The last line should have a newline.
missing_whitespace | JCR: Each comma, semicolon or colon should be followed by whitespace.
missing_whitespace_around_operator |
mute_string | Replace contents with 'xxx' to prevent syntax matching.
print_benchmark | Print benchmark numbers.
print_statistics | Print overall statistics (number of errors and warnings).
process_options | Process options passed either via arglist or via command line args.
python_3000_backticks | Backticks are removed in Python 3000.
python_3000_has_key | The {}.has_key() method will be removed in the future version of Python.
python_3000_not_equal | != can also be written <>, but this is an obsolete usage kept for backwards compatibility only.
python_3000_raise_comma | When raising an exception, use "raise ValueError('message')" instead of the older form "raise ValueError, 'message'".
readlines |
readlines |
reset_counters |
run_tests | Run all the tests from a file.
selftest | Test all check functions with test cases in docstrings.
tabs_obsolete |
tabs_or_spaces |
trailing_blank_lines |
trailing_whitespace |
whitespace_around_comma | Avoid extraneous whitespace in the following situations:
whitespace_around_named_parameter_equals | Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.
whitespace_around_operator | Avoid extraneous whitespace in the following situations:
whitespace_before_inline_comment | Separate inline comments by at least two spaces.
whitespace_before_parameters | Avoid extraneous whitespace in the following situations:

## Checker

Load a Python source file, tokenize it, check coding style.

### Derived from

object

### Class Attributes

None

### Class Methods

None

### Methods

Checker |
build_tokens_line | Build a logical line from tokens.
check_all | Run all checks on the input file.
check_logical | Build a line from tokens and run all logical checks on it.
check_physical | Run all physical checks on a raw input line.
readline | Get the next line from the input buffer.
readline_check_physical | Check and return the next physical line.
report_error | Report an error, according to options.
run_check | Run a check plugin.

### Static Methods

None

#### Checker (Constructor)

Checker(filename, lines=None)

#### Checker.build_tokens_line

build_tokens_line()

Build a logical line from tokens.

#### Checker.check_all

check_all(expected=None, line_offset=0)

Run all checks on the input file.

#### Checker.check_logical

check_logical()

Build a line from tokens and run all logical checks on it.

#### Checker.check_physical

check_physical(line)

Run all physical checks on a raw input line.

#### Checker.readline

readline()

Get the next line from the input buffer.

#### Checker.readline_check_physical

readline_check_physical()

Check and return the next physical line. This method can be used to feed tokenize.generate_tokens.

#### Checker.report_error

report_error(line_number, offset, text, check)

Report an error, according to options.

#### Checker.run_check

run_check(check, argument_names)

Run a check plugin.

Up

## _main

_main()

Parse options and run checks on Python source.

Up

## blank_lines

blank_lines(logical_line, blank_lines, indent_level, line_number, previous_logical, previous_indent_level, blank_lines_before_comment)
Up

## compound_statements

compound_statements(logical_line)
Up

## excluded

excluded(filename)

Check if options.exclude contains a pattern that matches filename.

Up

## expand_indent

expand_indent(line)

Return the amount of indentation. Tabs are expanded to the next multiple of 8.

>>> expand_indent(' ') 4 >>> expand_indent('\\t') 8 >>> expand_indent(' \\t') 8 >>> expand_indent(' \\t') 8 >>> expand_indent(' \\t') 16

Up

## extraneous_whitespace

extraneous_whitespace(logical_line)

Avoid extraneous whitespace in the following situations:

- Immediately inside parentheses, brackets or braces.

- Immediately before a comma, semicolon, or colon.

Okay: spam(ham[1], {eggs: 2}) E201: spam( ham[1], {eggs: 2}) E201: spam(ham[ 1], {eggs: 2}) E201: spam(ham[1], { eggs: 2}) E202: spam(ham[1], {eggs: 2} ) E202: spam(ham[1 ], {eggs: 2}) E202: spam(ham[1], {eggs: 2 })

E203: if x == 4: print x, y; x, y = y , x E203: if x == 4: print x, y ; x, y = y, x E203: if x == 4 : print x, y; x, y = y, x

Up

## filename_match

filename_match(filename)

Check if options.filename contains a pattern that matches filename. If options.filename is unspecified, this always returns True.

Up

## find_checks

find_checks(argument_name)

Find all globally visible functions where the first argument name starts with argument_name.

Up

## get_count

get_count(prefix='')

Return the total count of errors and warnings.

Up

## get_error_statistics

get_error_statistics()

Get error statistics.

Up

## get_statistics

get_statistics(prefix='')

Get statistics for message codes that start with the prefix.

prefix='' matches all errors and warnings prefix='E' matches all errors prefix='W' matches all warnings prefix='E4' matches all errors that have to do with imports

Up

## get_warning_statistics

get_warning_statistics()

Get warning statistics.

Up

## ignore_code

ignore_code(code)

Check if options.ignore contains a prefix of the error code. If options.select contains a prefix of the error code, do not ignore it.

Up

## imports_on_separate_lines

imports_on_separate_lines(logical_line)
Up

## indentation

indentation(logical_line, previous_logical, indent_char, indent_level, previous_indent_level)
Up

## input_dir

input_dir(dirname, runner=None)

Check all Python source files in this directory and all subdirectories.

Up

## input_file

input_file(filename)

Run all checks on a Python source file.

Up

## maximum_line_length

maximum_line_length(physical_line)

Limit all lines to a maximum of 79 characters.

There are still many devices around that are limited to 80 character lines; plus, limiting windows to 80 characters makes it possible to have several windows side-by-side. The default wrapping on such devices looks ugly. Therefore, please 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.

Up

## message

message(text)

Print a message.

Up

## missing_newline

missing_newline(physical_line)

JCR: The last line should have a newline.

Up

## missing_whitespace

missing_whitespace(logical_line)

JCR: Each comma, semicolon or colon should be followed by whitespace.

Okay: [a, b] Okay: (3,) Okay: a[1:4] Okay: a[:4] Okay: a[1:] Okay: a[1:4:2] E231: ['a','b'] E231: foo(bar,baz)

Up

## missing_whitespace_around_operator

missing_whitespace_around_operator(logical_line, tokens)
Up

## mute_string

mute_string(text)

Replace contents with 'xxx' to prevent syntax matching.

>>> mute_string('"abc"') '"xxx"' >>> mute_string("'''abc'''") "'''xxx'''" >>> mute_string("r'abc'") "r'xxx'"

Up

## print_benchmark

print_benchmark(elapsed)

Print benchmark numbers.

Up

## print_statistics

print_statistics(prefix='')

Print overall statistics (number of errors and warnings).

Up

## process_options

process_options(arglist=None)

Process options passed either via arglist or via command line args.

Up

## python_3000_backticks

python_3000_backticks(logical_line)

Backticks are removed in Python 3000. Use repr() instead.

Up

## python_3000_has_key

python_3000_has_key(logical_line)

The {}.has_key() method will be removed in the future version of Python. Use the 'in' operation instead, like: d = {"a": 1, "b": 2} if "b" in d: print d["b"]

Up

## python_3000_not_equal

python_3000_not_equal(logical_line)

!= can also be written <>, but this is an obsolete usage kept for backwards compatibility only. New code should always use !=. The older syntax is removed in Python 3000.

Up

## python_3000_raise_comma

python_3000_raise_comma(logical_line)

When raising an exception, use "raise ValueError('message')" instead of the older form "raise ValueError, 'message'".

The paren-using form is preferred because when the exception arguments are long or include string formatting, you don't need to use line continuation characters thanks to the containing parentheses. The older form will be removed in Python 3000.

Up

## readlines

readlines(filename)
Up

## readlines

readlines(filename)
Up

## reset_counters

reset_counters()
Up

## run_tests

run_tests(filename)

Run all the tests from a file.

A test file can provide many tests. Each test starts with a declaration. This declaration is a single line starting with '#:'. It declares codes of expected failures, separated by spaces or 'Okay' if no failure is expected. If the file does not contain such declaration, it should pass all tests. If the declaration is empty, following lines are not checked, until next declaration.

Examples:

* Only E224 and W701 are expected: #: E224 W701 * Following example is conform: #: Okay * Don't check these lines: #:

Up

## selftest

selftest()

Test all check functions with test cases in docstrings.

Up

## tabs_obsolete

tabs_obsolete(physical_line)
Up

## tabs_or_spaces

tabs_or_spaces(physical_line, indent_char)
Up

## trailing_blank_lines

trailing_blank_lines(physical_line, lines, line_number)
Up

## trailing_whitespace

trailing_whitespace(physical_line)
Up

## whitespace_around_comma

whitespace_around_comma(logical_line)

Avoid extraneous whitespace in the following situations:

- More than one space around an assignment (or other) operator to align it with another.

JCR: This should also be applied around comma etc. Note: these checks are disabled by default

Okay: a = (1, 2) E241: a = (1, 2) E242: a = (1,\t2)

Up

## whitespace_around_named_parameter_equals

whitespace_around_named_parameter_equals(logical_line)

Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.

Okay: def complex(real, imag=0.0): Okay: return magic(r=real, i=imag) Okay: boolean(a == b) Okay: boolean(a != b) Okay: boolean(a <= b) Okay: boolean(a >= b)

E251: def complex(real, imag = 0.0): E251: return magic(r = real, i = imag)

Up

## whitespace_around_operator

whitespace_around_operator(logical_line)

Avoid extraneous whitespace in the following situations:

- More than one space around an assignment (or other) operator to align it with another.

Okay: a = 12 + 3 E221: a = 4 + 5 E222: a = 4 + 5 E223: a = 4\t+ 5 E224: a = 4 +\t5

Up

## whitespace_before_inline_comment

whitespace_before_inline_comment(logical_line, tokens)

Separate inline comments by at least two spaces.

An inline comment is a comment on the same line as a statement. Inline comments should be separated by at least two spaces from the statement. They should start with a # and a single space.

Okay: x = x + 1 # Increment x Okay: x = x + 1 # Increment x E261: x = x + 1 # Increment x E262: x = x + 1 #Increment x E262: x = x + 1 # Increment x

Up

## whitespace_before_parameters

whitespace_before_parameters(logical_line, tokens)

Avoid extraneous whitespace in the following situations:

- Immediately before the open parenthesis that starts the argument list of a function call.

- Immediately before the open parenthesis that starts an indexing or slicing.

Okay: spam(1) E211: spam (1)

Okay: dict['key'] = list[index] E211: dict ['key'] = list[index] E211: dict['key'] = list [index]

Up