Package cheesecake :: Module pep8
[show private | hide private]
[frames | no frames]

Module cheesecake.pep8

Check Python source code formatting, according to PEP 8: http://www.python.org/dev/peps/pep-0008/

For usage and a list of options, try this: $ python pep8.py -h

This program and its regression test suite live here: http://svn.browsershots.org/trunk/devtools/pep8/ http://trac.browsershots.org/browser/trunk/devtools/pep8/

Groups of errors and warnings: E errors W warnings 100 indentation 200 whitespace 300 blank lines 400 imports 500 line length 600 deprecation

You can add checks to this program by writing plugins. Each plugin is a simple function that is called for each line of source code, either physical or logical.

Physical line: - Raw line of text from the input file.

Logical line: - Multi-line statements converted to a single line. - Stripped left and right. - Contents of strings replaced with 'xxx' of same length. - Comments removed.

The check function requests physical or logical lines by the name of the first argument:

def maximum_line_length(physical_line) def extraneous_whitespace(logical_line) def indentation(logical_line, indent_level, state)

The last example above demonstrates how check plugins can request additional information with extra arguments. All attributes of the Checker object are available. Some examples:

lines: a list of the raw lines from the input file tokens: the tokens that contribute to this logical line state: dictionary for passing information across lines indent_level: indentation (with tabs expanded to multiples of 8)

The docstring of each check function shall be the relevant part of text from PEP 8. It is printed if the user enables --show-pep8.


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

Function Summary
  blank_lines(logical_line, state, indent_level)
Separate top-level function and class definitions with two blank lines.
  excluded(filename)
Check if options.exclude contains a pattern that matches filename.
  expand_indent(line)
Return the amount of indentation.
  extraneous_whitespace(logical_line)
Avoid extraneous whitespace in the following situations:
  filename_match(filename)
Check if options.filename contains a pattern that matches filename.
  find_checks(argument_name)
Find all globally visible functions where the first argument name starts with argument_name.
  get_error_statistics()
Get error statistics.
  get_statistics(prefix)
Get statistics for message codes that start with the prefix.
  get_warning_statistics()
Get warning statistics.
  ignore_code(code)
Check if options.ignore contains a prefix of the error code.
  imports_on_separate_lines(logical_line)
Imports should usually be on separate lines.
  indentation(logical_line, indent_level, state)
Use 4 spaces per indentation level.
  input_dir(dirname)
Check all Python source files in this directory and all subdirectories.
  input_file(filename)
Run all checks on a Python source file.
  maximum_line_length(physical_line)
Limit all lines to a maximum of 79 characters.
  message(text)
Print a message.
  mute_string(text)
Replace contents with 'xxx' to prevent syntax matching.
  print_benchmark(elapsed)
Print benchmark numbers.
  print_statistics(prefix)
Print overall statistics (number of errors and warnings).
  process_options(arglist)
Process options passed either via arglist or via command line args.
  python_3000_has_key(logical_line)
The {}.has_key() method will be removed in the future version of Python.
  python_3000_raise_comma(logical_line)
When raising an exception, use "raise ValueError('message')" instead of the older form "raise ValueError, 'message'".
  tabs_obsolete(physical_line)
For new projects, spaces-only are strongly recommended over tabs.
  tabs_or_spaces(physical_line, state)
Never mix tabs and spaces.
  trailing_whitespace(physical_line)
JCR: Trailing whitespace is superfluous.
  whitespace_around_operator(logical_line)
Avoid extraneous whitespace in the following situations:
  whitespace_before_parameters(logical_line, tokens)
Avoid extraneous whitespace in the following situations:

Variable Summary
str __revision__ = '$Rev: 930 $'
str __version__ = '0.2.0'
NoneType args = None                                                                  
str default_exclude = '.svn,CVS,*.pyc,*.pyo'
list operators = ['+', '-', '*', '/', '%', '^', '&', '|', '='...
NoneType options = None                                                                  

Function Details

blank_lines(logical_line, state, indent_level)

Separate top-level function and class definitions with two blank lines.

Method definitions inside a class are separated by a single blank line.

Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations).

Use blank lines in functions, sparingly, to indicate logical sections.

excluded(filename)

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

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

extraneous_whitespace(logical_line)

Avoid extraneous whitespace in the following situations:

  • Immediately inside parentheses, brackets or braces.
  • Immediately before a comma, semicolon, or colon.

filename_match(filename)

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

find_checks(argument_name)

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

get_error_statistics()

Get error 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

get_warning_statistics()

Get warning statistics.

ignore_code(code)

Check if options.ignore contains a prefix of the error code.

imports_on_separate_lines(logical_line)

Imports should usually be on separate lines.

indentation(logical_line, indent_level, state)

Use 4 spaces per indentation level.

For really old code that you don't want to mess up, you can continue to use 8-space tabs.

input_dir(dirname)

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

input_file(filename)

Run all checks on a Python source file.

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.

message(text)

Print a message.

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'"

print_benchmark(elapsed)

Print benchmark numbers.

print_statistics(prefix='')

Print overall statistics (number of errors and warnings).

process_options(arglist=None)

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

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"]

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.

tabs_obsolete(physical_line)

For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.

tabs_or_spaces(physical_line, state)

Never mix tabs and spaces.

The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

trailing_whitespace(physical_line)

JCR: Trailing whitespace is superfluous.

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.

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.

Variable Details

__revision__

Type:
str
Value:
'$Rev: 930 $'                                                          

__version__

Type:
str
Value:
'0.2.0'                                                                

args

Type:
NoneType
Value:
None                                                                  

default_exclude

Type:
str
Value:
'.svn,CVS,*.pyc,*.pyo'                                                 

operators

Type:
list
Value:
['+', '-', '*', '/', '%', '^', '&', '|', '=']                          

options

Type:
NoneType
Value:
None                                                                  

Generated by Epydoc 2.1 on Fri Feb 9 02:15:13 2007 http://epydoc.sf.net