-
Notifications
You must be signed in to change notification settings - Fork 11
/
pep8_notes
203 lines (157 loc) · 4.18 KB
/
pep8_notes
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
"""
Function definitions. Extra indentation is needed.
"""
def function(
var1, var2, var3, var4,
var5, var6):
print "Different indentation level"
"""
Function call. Extra indentation is needed.
"""
function(
var1, var2, var3, var4,
var5, var6)
"""
Multi-line if-else indentation. Extra indentation is not needed.
"""
if (a and b
and c and d):
print "Inside IF Loop"
"""
Closing parentheses for initialized variables among others (do not know what)
"""
python_list = [
1, 2, 3,
4, 5, 6
]
python_dict = {
'key1':'val1', 'key2':'val2',
'key3':'val3', 'key4':'val4'
}
"""
Command line switches to be used while running python code
"""
tt Throws an error IF tabs and spaces are mixed while indenting code.
"""
Maximum line length
"""
79 characters inclusive of line breaks. Take care while writing code over
multiple lines.
72 characters for Python Docstrings.
"""
Word wrap
"""
- Ideally use parentheses and spacing to make it happen naturally.
- If not, and it has to be multiline use backslashes but minimize its use.
with open('longpath/file1') as file_1, \
open('longpath/file2') as file_2:
file_2.write(file_1.read())
"""
Line break after the operator (and,or)
"""
def func():
if (a and b and
c and d):
if (A or B or
C or D):
"""
Use newlines logically.
"""
class booClass
def classFunc1():
print "Inside func1"
def classFunc2():
print "Inside func2"
"""
Encoding should always be UTF-8
"""
Set encoding globally to UTF-8 on your system. So any editor you use will
display characters using that encoding. On Linux set the environment variable
$LANG.
"""
Imported modules should be on separate lines. If you import only specific
submodules, they can be on the same line.
Logically separate System imports, thirdparty imports and your own custom
imports by a new line each.
Use absolute paths for custom imports as far as possible.
DO NOT use wildcard imports.
"""
import os
import sys
from subprocess import Popen,PIPE
import bouncycastle.ssl
import com.mypackage.mylibrary1
import com.mypackage.mylibrary2
#from module import *
def method1:
def method2:
"""
Use spaces well between brackets, punctuations, variables and operators.
Dont use spaces inside arguments with the = operator.
"""
list=['a', 'b']
dict={'a':'va', 'b':'vb'}
if a==1 and b==2:
print a, b
a = 1
b = 2
cccc = 34
i = i + 2
i += 1
if i == 4:
print i
c = (a+b) * (a-b)
if (a and b) or (c and d):
print a, b, c, d
def func(a, b=0):
print 'Inside func'
"""
No compound statements. Statements with similar functionality always on
separate lines.
"""
import sys
import os
import re
i=1
j=2
"""
Comments should always be full sentences with a capital first word. End with a
fullstop. If you start a new comment, use 2 spaces after the end of the
sentence. Single space after the # before you start your comment.
Don't use inline comments unless absolutely necessary and don't state the
obvious stuff.
"""
# This is a comment.
# This is a comment. And here is a continuation of the comment.
"""
Naming conventions.
l, o, i should not be ever used as variable names.
Module names should be lowercase without underscores if possible.
Class names should use CamelCase.
Exception names should use CamelCase.
Global variables should ideally be used inside a single module. Names of global
variables should be lowercase without underscores if possible.
Function names should be lowercase without underscores if possible.
Constants are all CAPS separated by underscores if possible.
Classes and interfaces NOT DISCUSSED RIGHT NOW. Will fill this up when I learn
to use them better.
"""
module modulename
class ClassName
Exception RaiseError
global globvar
def function:
CONST_AGE = 42
"""
Programming recommendations.
"""
Do not break other implementations of Python. (Do this on a case by case basis)
if a is None:
if b is not None:
if a:
if not b:
if isinstance(obj, int):
Mention specific Exceptions as far as you can
Use with to ensure all resources are cleant up as soon as possible
If overriding eq, also override all the other operations, even if its with a
blank method. Decorators which generate these blank methods are available.