-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_style_examples.py
More file actions
289 lines (242 loc) · 5.76 KB
/
python_style_examples.py
File metadata and controls
289 lines (242 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
###############################################################################
# Continuation lines
# Yes:
# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# More indentation included to distinguish this 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)
# No:
# 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)
# Acceptable:
# No extra indentation.
if (this_is_one_thing and
that_is_another_thing):
do_something()
# Add a comment, which will provide some distinction
# in editors supporting syntax highlighting.
if (this_is_one_thing and
that_is_another_thing):
# Since both conditions are true, we can do something.
do_something()
# Add some extra indentation on the conditional continuation line.
if (this_is_one_thing
and that_is_another_thing):
do_something()
# Closing brackets (all acceptable)
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
my_list = [
1, 2, 3,
4, 5, 6,
]
result = some_function_that_takes_arguments(
'a', 'b', 'c',
'd', 'e', 'f',
)
###############################################################################
# Line split
with open('/path/to/some/file/you/want/to/read') as file_1, \
open('/path/to/some/file/being/written', 'w') as file_2:
file_2.write(file_1.read())
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
object.do_something().do_something_else()\
.what_am_i_doing()
###############################################################################
# Imports
# Yes:
import os
import sys
from subprocess import Popen, PIPE
# No:
import sys, os
# Avoid:
from module import *
# imports order
import os
from rest_framework import serializers
from .models import Category, Tag, Article
###############################################################################
# Whitespace
# Yes:
spam(ham[1], {eggs: 2})
# No:
spam( ham[ 1 ], { eggs: 2 } )
# Yes:
if x == 4:
print(x, y)
x, y = y, x
# No:
if x == 4 :
print(x , y)
x , y = y , x
# Yes:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset:upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset:upper + offset]
# No:
ham[lower + offset:upper + offset]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]
# Yes:
spam(1)
dct['key'] = lst[index]
# No:
spam (1)
dct ['key'] = lst [index]
# Yes:
x = 1
y = 2
long_variable = 3
# No:
x = 1
y = 2
long_variable = 3
# Yes:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
# No:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
# No spaces around = in keyword arguments or default values
def complex(real, imag=0.0):
return magic(r=real, i=imag)
# Yes:
if foo == 'blah':
do_blah_thing()
do_one()
do_two()
do_three()
# Rather not:
if foo == 'blah': do_blah_thing()
do_one(); do_two(); do_three()
###############################################################################
# Comments
# No:
x = x + 1 # Increment x
# Maybe:
x = x + 1 # Compensate for border
###############################################################################
# Recommendations
# Yes:
if foo is not None:
pass
# No:
if not foo is None:
pass
# Yes:
def f(x): return 2*x
# No:
f = lambda x: 2*x
# Yes:
try:
value = collection[key]
except KeyError:
return key_not_found(key)
else:
return handle_value(value)
# No:
try:
# Too broad!
return handle_value(collection[key])
except KeyError:
# Will also catch KeyError raised by handle_value()
return key_not_found(key)
# Yes:
if foo.startswith('bar'):
pass
# No:
if foo[:3] == 'bar':
pass
# Yes:
if not seq:
pass
if seq:
pass
# No:
if len(seq):
pass
if not len(seq):
pass
# Yes:
if greeting:
pass
# No:
if greeting == True:
pass
# Worse:
if greeting is True:
pass
###############################################################################
# Django
# use “action words” in docstrings
def foo():
"""
Calculates something and returns the result.
"""
pass
# Yes:
class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=40)
class Meta:
verbose_name_plural = 'people'
# No:
class Person(models.Model):
class Meta:
verbose_name_plural = 'people'
FirstName = models.CharField(max_length=20)
Last_Name = models.CharField(max_length=40)
# Yes:
# {{ foo }}
# No:
# {{foo}}
# Choices
class MyModel(models.Model):
DIRECTION_UP = 'U'
DIRECTION_DOWN = 'D'
DIRECTION_CHOICES = (
(DIRECTION_UP, 'Up'),
(DIRECTION_DOWN, 'Down'),
)