Rules
- Blank lines
- Surround top-level function and class definitions with two blank lines.
- Method definitions inside a class are surrounded by a single blank line.
- Blank line doesn't contains whitespace
- Input a whitespace after ', input a whitespace after ':'
- Line too long (131 > 79 characters)
- Limit all lines to a maximum of 79 characters
- Limiting the required editor window width makes it possible to have several files open side-by-side, and works well when using code review tools that present the two versions in adjacent columns.
- Continuation lines should align wrapped elements either vertically using Python's implicit line joining inside parentheses, brackets and braces, or using a handing indent. When using a hanging indent the following shoudl be considered; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line:
# Correct:
# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Add 4 spaces (an extra level of indentation) to distinguish arguments from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# Hanging indents should add a level.
foo = long_function_name(
var_one, var_two,
var_three, var_four)
# Wrong:
# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Further indentation required as indentation is not distinguishable.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
- Imports should usually be on separate lines:
# Correct:
import os
import sys
# Wrong:
import sys, os
It's okay to say this though:
# Correct:
from subprocess import Popen, PIPE
- 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.
- Whitespace in Expressions and Statements
If operators with different priorities are used, consider adding whitespace around the operators with the lowest priority(ies). Use your own
judgment; however, never use more than one space, and always have the same amount of whitespace on both sides of a binary operator:
# Correct:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
# Wrong:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
- Don't use spaces around the = sign when used to indicate a keyword argument, or when used to indicate a default value for an
unannotated function parameter:
# Correct:
def complex(real, imag=0.0):
return magic(r=real, i=imag)
# Wrong:
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
- Check if vaiable equals a constant
- You don't need to explicity compare a value to True, or None, or 0, you can just add it to the if statement.
# Bad:
if attr == True:
print 'True!'
if attr == None:
print 'attr is None!'
# Good:
# Just check the value
if attr:
print 'attr is truthy!'
# or check for the opposite
if not attr:
print 'attr is falsey!'
# or, since None is considered false, explicitly check for it
if attr is None:
print 'attr is None!'
- Comments that contradict the code are worse than no comments. Always make a priority of keeping the
comments up-to-date when the code changes!
Comments should be complete sentences. The first word should be capitalized, unless it is an identifier that begins with a lower case letter
(never alter the case of identifiers!)
- Python rules:
- Classes are PascalCase
- CONSTANTS are UPPER_CASE
- Variables are snake_case
- methods are snake_case
- SQLAlchemy
- xxx.is_(None) instead of xxx == None,
- xxx.isnot_(None) instead of xxx != None
Restful API
HTTP Verb |
Path |
Controller#Action |
Used for |
GET |
/photos |
photos#index |
display a list of all photos |
GET |
/photos/new |
photos#new |
return an HTML form for creating a new photo |
POST |
/photos |
photos#create |
create a new photo |
GET |
/photos/:id |
photos#show |
display a specific photo |
GET |
/photos/:id/edit |
photos#edit |
return an HTML form for editing a photo |
PUT |
/photos/:id |
photos#update |
update a specific photo |
DELETE |
/photos/:id |
photos#destroy |
delete a specific photo |