Difference between revisions of "Documentation/4.1/Developers/Style Guide/Python"

From Slicer Wiki
Jump to: navigation, search
(Outlined PEP 8 briefly and added links)
 
(Prepend documentation/versioncheck template. See http://na-mic.org/Mantis/view.php?id=2887)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 +
<noinclude>{{documentation/versioncheck}}</noinclude>
 
Follow PEP 8 standards: [http://www.python.org/dev/peps/pep-0008/ Official PEP 8 Guidelines]
 
Follow PEP 8 standards: [http://www.python.org/dev/peps/pep-0008/ Official PEP 8 Guidelines]
  
Line 26: Line 27:
 
* Avoid extraneous whitespaces
 
* Avoid extraneous whitespaces
 
* Naming conventions
 
* Naming conventions
 +
** [http://www.python.org/dev/peps/pep-0008/#package-and-module-names Package and Module Names]
 
** When PEP 8 and Slicer naming conventions conflict, Slicer wins
 
** When PEP 8 and Slicer naming conventions conflict, Slicer wins
  
 
== Checking your code ==
 
== Checking your code ==
  
There is a handy command line tool to check your code against the PEP 8 standard called pep8.  It has options to turn off specific warnings and errors in case you (almost always) have to break from the standard.  You can download and install it from it's [[https://github.com/jcrocholl/pep8 Github repo.]]
+
There is a handy command line tool to check your code against the PEP 8 standard called pep8.  It has options to turn off specific warnings and errors in case you (almost always) have to break from the standard.  You can download and install it from it's [https://github.com/jcrocholl/pep8 Github repo]

Latest revision as of 07:24, 14 June 2013

Home < Documentation < 4.1 < Developers < Style Guide < Python


For the latest Slicer documentation, visit the read-the-docs.


Follow PEP 8 standards: Official PEP 8 Guidelines

Rules

  • Indentation is --- 4 spaces per level ---
    • Do not use tabs, EVER! Those who insist on using tabs for indentation will be a pariah in the community and summarily shot on sight.
  • Text encoding
    • ASCII or Latin-1 (Python 2+)
    • UTF-8 preferred, Latin-1 acceptable (Python 3)
  • Comparisons
    • To singletons (e.g. None)
      • Use 'is' or 'is not', never equality operations
    • To booleans (True, False)
      • Don't ever compare with True or False (For further explanation, see PEP 8)

Guidelines

  • Maximum line length is 79 characters
  • Prefix class definitions with two blank lines
  • Imports
    • Grouped in order of scope/commonallity
      1. Standard library imports
      2. Related third party imports
      3. Local apps/library specific imports
    • One package per line (with or without multiple function/module/class imports from the package)
  • Avoid extraneous whitespaces
  • Naming conventions

Checking your code

There is a handy command line tool to check your code against the PEP 8 standard called pep8. It has options to turn off specific warnings and errors in case you (almost always) have to break from the standard. You can download and install it from it's Github repo