Skip to content

Commit 5d970e1

Browse files
committed
Adding some coding conventions that we follow, but never documented.
1 parent 761b0b2 commit 5d970e1

File tree

1 file changed

+91
-4
lines changed

1 file changed

+91
-4
lines changed

coding.rst

+91-4
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,105 @@ that the code is correct to the best of our knowledge. See
5858
Python
5959
------
6060

61-
We do what others do:
61+
We do what others in the python community have established:
6262

6363
* We follow PEP8_.
6464
* We test using check.py_ which combines `pep8.py` and `pyflakes`.
65-
* Pocoo_ has good guidelines as well, let's steal them.
65+
* We follow Pocoo_'s extensions of PEP8_ as they are well thought out.
6666

67-
Also, spaces matter:
67+
Import Statements
68+
^^^^^^^^^^^^^^^^^
69+
70+
We expand on PEP8_'s suggestions for import statements. These greatly improve
71+
ones ability to ascertain what is and isn't available in a given file.
72+
73+
Import one module per import statement::
74+
75+
import os
76+
import sys
77+
78+
not::
79+
80+
import os, sys
81+
82+
Separate imports into groups with a line of whitespace:
83+
standard library; Django (or framework); third-party; and local imports::
84+
85+
import os
86+
import sys
87+
88+
from django.conf import settings
89+
90+
import pyquery
91+
92+
from myapp import models, views
93+
94+
95+
Alphabetize your imports, it will make your code easier to scan. See how terrible this is::
96+
97+
import cows
98+
import kittens
99+
import bears
100+
101+
A simple sort::
102+
103+
import bears
104+
import cows
105+
import kittens
106+
107+
Imports on top, ``from``-imports below::
108+
109+
import x
110+
import y
111+
import z
112+
from bears import pandas
113+
from xylophone import bar
114+
from zoos import lions
115+
116+
That's loads easier to read than::
117+
118+
from bears import pandas
119+
import x
120+
from xylophone import bar
121+
import y
122+
import z
123+
from zoos import lions
124+
125+
126+
Lastly, when importing things into your namespace from a package use an alphabetized
127+
``CONSTANT``, ``Class``, ``var`` order::
128+
129+
from models import DATE, TIME, Dog, Kitteh, upload_pets
130+
131+
132+
If possible though, it may be easier to import the entire package, especially for methods
133+
as it help answers the question, "where did ``you`` come from?"
134+
135+
Bad::
136+
137+
from foo import you
138+
139+
140+
def my_code():
141+
you() # wait, is this defined in this file?
142+
143+
144+
Good::
145+
146+
import foo
147+
148+
149+
def my_code():
150+
foo.you() # oh you...
151+
152+
153+
Whitespace matters
154+
^^^^^^^^^^^^^^^^^^
68155

69156
* Use 4 spaces, not 2---it increases legibility considerably.
70157
* Never use tabs---history has shown that we cannot handle them.
71158

72-
Use single quotes unless you need double (or triple) quotes::
159+
Use single quotes unless double (or triple) quotes would be an improvement::
73160

74161
'this is good'
75162

0 commit comments

Comments
 (0)