Python Coding Standards » History » Version 2
Neil Voss, 03/19/2010 02:19 PM
1 | 1 | Neil Voss | h1. Python Coding Standards |
---|---|---|---|
2 | |||
3 | This document is a list of python coding standards. To add a new standard copy the template below and modify it. |
||
4 | |||
5 | 2 | Neil Voss | see also http://emg.nysbc.org/wiki/index.php/Appionscripts_formatting_rules |
6 | |||
7 | 1 | Neil Voss | ---- |
8 | |||
9 | h2. +Name of Coding Standard+ |
||
10 | |||
11 | h3. Definition |
||
12 | |||
13 | What is the coding standard |
||
14 | |||
15 | h3. Justification |
||
16 | |||
17 | Why is the coding standard important |
||
18 | |||
19 | h3. Example |
||
20 | |||
21 | GOOD:<pre> |
||
22 | this is a good example code |
||
23 | </pre> |
||
24 | |||
25 | BAD:<pre> |
||
26 | this is a bad example code |
||
27 | </pre> |
||
28 | |||
29 | ---- |
||
30 | |||
31 | h1. Python Coding Standards for AMI |
||
32 | |||
33 | ---- |
||
34 | |||
35 | 2 | Neil Voss | h2. +Use tabs instead of spaces+ |
36 | |||
37 | h3. Definition |
||
38 | |||
39 | Use tabs instead of spaces for inline code |
||
40 | |||
41 | h3. Justification |
||
42 | |||
43 | It is important to be consistent. People like different sizes of columns, so like 8 spaces, others 4, 3, or 2. With tabs each individual can customize their viewer. |
||
44 | |||
45 | h3. Example |
||
46 | |||
47 | GOOD:<pre> |
||
48 | if True: |
||
49 | <tab>while True: |
||
50 | <tab><tab>print "tab" |
||
51 | <tab>break |
||
52 | </pre> |
||
53 | |||
54 | BAD:<pre> |
||
55 | if True: |
||
56 | while True: |
||
57 | print "tab" |
||
58 | break |
||
59 | </pre> |
||
60 | |||
61 | ---- |
||
62 | |||
63 | 1 | Neil Voss | h2. +Checking beginning or ending of strings+ |
64 | |||
65 | h3. Definition |
||
66 | |||
67 | Use ''.startswith() and ''.endswith() instead of string slicing to check for prefixes or suffixes. |
||
68 | |||
69 | h3. Justification |
||
70 | |||
71 | startswith() and endswith() are cleaner and less error prone. |
||
72 | |||
73 | h3. Example |
||
74 | |||
75 | GOOD:<pre> |
||
76 | if foo.startswith('bar'): |
||
77 | </pre> |
||
78 | |||
79 | BAD:<pre> |
||
80 | if foo[:3] == 'bar': |
||
81 | </pre> |
||
82 | |||
83 | ---- |
||
84 | |||
85 | h2. +Never use @from module import *@+ |
||
86 | |||
87 | h3. Definition |
||
88 | |||
89 | Never use @from module import *@, use @import module@ instead |
||
90 | |||
91 | h3. Justification |
||
92 | |||
93 | It is hard to track where functions come from when @import *@ is used |
||
94 | |||
95 | h3. Example |
||
96 | |||
97 | GOOD:<pre> |
||
98 | import numpy |
||
99 | a = numpy.ones((3,3)) |
||
100 | </pre> |
||
101 | |||
102 | BAD:<pre> |
||
103 | from numpy import * |
||
104 | a = ones((3,3)) |
||
105 | 2 | Neil Voss | </pre> |
106 | |||
107 | ---- |
||
108 | |||
109 | h2. +Appion image data naming conventions+ |
||
110 | |||
111 | h3. Definition |
||
112 | |||
113 | # never use 'image' or 'img' |
||
114 | # use 'imgdict' for image dictionaries |
||
115 | # use 'imgarray' for image numarrays |
||
116 | # use 'imgname' for image filenames |
||
117 | # use 'imgtree' for the main list of image dictionaries |
||
118 | # use 'imglist' for a list of image data |
||
119 | |||
120 | h3. Justification |
||
121 | |||
122 | If you are consistent with your names people can read your code. |
||
123 | |||
124 | h3. Example |
||
125 | |||
126 | GOOD:<pre> |
||
127 | for imgdict in imgtree: |
||
128 | imgarray = imgdict['image'] |
||
129 | imgname = imgdict['filename'] |
||
130 | </pre> |
||
131 | |||
132 | BAD:<pre> |
||
133 | for image in imgs: |
||
134 | array = image['image'] |
||
135 | name = image['filename'] |
||
136 | 1 | Neil Voss | </pre> |