annotate doc/v2_planning/API_coding_style.txt @ 1159:531e77fb67f2

coding_style: Moved more elements to official 'API'
author Olivier Delalleau <delallea@iro>
date Fri, 17 Sep 2010 12:05:14 -0400
parents d7192e52653e
children 4f1b9e0a1377
rev   line source
1143
fa1715e759e3 Added API file for coding style committee (now we just need to fill it)
Olivier Delalleau <delallea@iro>
parents:
diff changeset
1 =========================
fa1715e759e3 Added API file for coding style committee (now we just need to fill it)
Olivier Delalleau <delallea@iro>
parents:
diff changeset
2 Coding Style Guidelines
fa1715e759e3 Added API file for coding style committee (now we just need to fill it)
Olivier Delalleau <delallea@iro>
parents:
diff changeset
3 =========================
fa1715e759e3 Added API file for coding style committee (now we just need to fill it)
Olivier Delalleau <delallea@iro>
parents:
diff changeset
4
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
5 Main Goals
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
6 ==========
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
7
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
8 * Code should be compatible with Python 2.4 and above (using 2to3 for
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
9 conversion to Python 3.x). This may not be possible in the short term
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
10 for Theano-dependent code.
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
11
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
12 * Code should be easy to read, understand and update by developers and
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
13 users.
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
14
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
15 * Code should be well-documented and well-tested.
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
16
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
17 Python Coding Guidelines
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
18 ========================
1143
fa1715e759e3 Added API file for coding style committee (now we just need to fill it)
Olivier Delalleau <delallea@iro>
parents:
diff changeset
19
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
20 Official Guidelines
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
21 -------------------
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
22
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
23 Source Material
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
24 ~~~~~~~~~~~~~~~
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
25
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
26 The four main documents describing our Python coding guidelines are:
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
27 * `PEP 8 -- Style Guide for Python Code
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
28 <http://www.python.org/dev/peps/pep-0008>`_
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
29 * `PEP 257 -- Docstring Conventions
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
30 <http://www.python.org/dev/peps/pep-0257>`_
1147
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
31 * `Numpy Docstring Standard
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
32 <http://projects.scipy.org/numpy/wiki/CodingStyleGuidelines#docstring-standard>`_
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
33 * `Google Python Style Guide
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
34 <http://google-styleguide.googlecode.com/svn/trunk/pyguide.html>`_
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
35
1147
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
36
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
37 However, there are a few points mentioned in those documents that we decided
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
38 to do differently:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
39
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
40 * Use only one space (not two) after a sentence-ending period in comments.
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
41
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
42 * You do not need to add an extra blank line before the closing quotes of
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
43 a multi-line docstring.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
44
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
45 .. code-block:: python
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
46
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
47 # Good.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
48 """This is a multi-line docstring.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
49
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
50 Which means it has more than one line.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
51 """
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
52
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
53 # Bad.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
54 """This is a multi-line docstring.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
55
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
56 Which means it has more than one line.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
57
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
58 """
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
59
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
60 Excerpts
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
61 ~~~~~~~~
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
62
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
63 We emphasize here a few important topics that are found in the official
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
64 guidelines:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
65
1159
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
66 * Only use ASCII characters in code files.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
67
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
68 * Code indent must be done with four blank characters (no tabs).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
69
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
70 * Limit lines to 79 characters.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
71
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
72 * Naming conventions: ``ClassName``, ``TOP_LEVEL_CONSTANT``,
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
73 ``everything_else``.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
74
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
75 * Comments should start with a capital letter (unless the first word is a
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
76 code identifier) and end with a period (short inline comments may skip
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
77 the period at the end).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
78
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
79 * Imports should be listed in alphabetical order. It makes it easier to
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
80 verify that something is imported, and avoids duplicated imports.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
81
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
82 * Use absolute imports only. This is compatible across a wider range of
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
83 Python versions, and avoids confusion about what is being
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
84 imported.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
85
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
86 * Avoid using lists if all you care about is iterating on something. Using
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
87 lists:
1159
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
88
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
89 - uses more memory (and possibly more CPU if the code may break out of
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
90 the iteration),
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
91 - can lead to ugly code when converted to Python 3 with 2to3,
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
92 - can have a different behavior if evaluating elements in the list has
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
93 side effects (if you want these side effects, make it explicit by
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
94 assigning the list to some variable before iterating on it).
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
95
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
96 +------------------------+------------------------+
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
97 | Iterative version | List version |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
98 +========================+========================+
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
99 | .. code-block:: python | .. code-block:: python |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
100 | | |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
101 | my_dict.iterkeys | my_dict.keys |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
102 | my_dict.itervalues | my_dict.values |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
103 | my_dict.iteritems | my_dict.items |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
104 +------------------------+------------------------+
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
105 | .. code-block:: python | .. code-block:: python |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
106 | | |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
107 | itertools.ifilter | filter |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
108 | itertools.imap | map |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
109 | itertools.izip | zip |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
110 +------------------------+------------------------+
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
111 | .. code-block:: python | .. code-block:: python |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
112 | | |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
113 | xrange | range |
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
114 +------------------------+------------------------+
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
115
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
116 Code example with ``map``:
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
117
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
118 .. code-block:: python
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
119
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
120 # Good.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
121 for f_x in imap(f, x):
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
122 ...
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
123 all_f_x = map(f, x)
1159
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
124 map(f, x) # f has some side effect.
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
125 # Bad.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
126 for element in map(f, x):
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
127 ...
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
128 imap(f, x)
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
129
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
130 * Generally prefer list comprehensions to ``map`` / ``filter``, as the former are
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
131 easier to read.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
132
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
133 .. code-block:: python
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
134
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
135 # Good.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
136 non_comments = [line.strip() for line in my_file.readlines()
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
137 if not line.startswith('#')]
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
138 # Bad.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
139 non_comments = map(str.strip,
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
140 ifilter(lambda line: not line.startswith('#'),
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
141 my_file.readlines()))
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
142
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
143 * Use ``in`` on container objects instead of using class-specific methods:
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
144 it is easier to read and may allow you to re-use your code with different
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
145 container types.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
146
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
147 .. code-block:: python
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
148
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
149 # Good.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
150 has_key = key in my_dict
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
151 has_substring = substring in my_string
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
152 # Bad.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
153 has_key = my_dict.has_key(key)
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
154 has_substring = my_string.find(substring) >= 0
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
155
1159
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
156 * Do not use mutable arguments as default values. Instead, use a helper
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
157 function (conditional expressions are forbidden at this point, see
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
158 below).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
159
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
160 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
161
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
162 # Good.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
163 def f(array=None):
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
164 array = pylearn.if_none(array, [])
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
165 ...
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
166 # Bad.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
167 def f(array=[]): # Dangerous if `array` is modified down the road.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
168 ...
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
169
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
170 * Use a leading underscore '_' in names of internal attributes / methods,
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
171 but avoid the double underscore '__' unless you know what you are
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
172 doing.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
173
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
174
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
175 Additional Recommendations
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
176 --------------------------
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
177
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
178 Things you should do even if they are not listed in official guidelines:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
179
1159
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
180 * No conditional expression (not supported in Python 2.4). These are
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
181 expressions of the form ``x = y if condition else z``.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
182
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
183 * Use ``//`` for integer division and ``/ float(...)`` if you want the
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
184 floating point operation (for readability and compatibility across all
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
185 versions of Python).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
186
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
187 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
188
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
189 # Good.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
190 n_samples_per_split = n_samples // n_splits
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
191 mean_x = sum(x) / float(len(x))
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
192 # Bad.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
193 n_samples_per_split = n_samples / n_splits
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
194 mean_x = sum(x) / len(x)
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
195
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
196 * Always raise an exception with ``raise MyException(args)`` where ``MyException``
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
197 inherits from ``Exception``. This is required for compatibility across
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
198 all versions of Python.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
199
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
200 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
201
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
202 # Good.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
203 raise NotImplementedError('The Pylearn team is too lazy.')
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
204 # Bad.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
205 raise NotImplementedError, 'The Pylearn team is too lazy.'
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
206 raise 'The Pylearn team is too lazy to implement this.'
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
207
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
208 * Use either ``try ... except`` or ``try ... finally``, but do not mix
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
209 ``except`` with ``finally`` (which is not supported in Python 2.4).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
210 You can however embed one into the other to mimic the ``try ... except ...
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
211 finally`` behavior.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
212
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
213 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
214
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
215 # Good.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
216 try:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
217 try:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
218 something_that_may_fail()
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
219 except SomeError:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
220 do_something_if_it_failed()
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
221 finally:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
222 always_do_this_regardless_of_what_happened()
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
223 # Bad.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
224 try:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
225 something_that_may_fail()
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
226 except SomeError:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
227 do_something_if_it_failed()
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
228 finally:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
229 always_do_this_regardless_of_what_happened()
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
230
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
231 * Do not use the ``all`` and ``any`` builtin functions (they are not supported
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
232 in Python 2.4). Instead, import them from ``theano.gof.python25`` (or
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
233 use ``numpy.all`` / ``numpy.any`` for array data).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
234
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
235 * Do not use the ``hashlib`` module (not supported in Python 2.4). We will
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
236 probably provide a wrapper around it to be compatible with all Python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
237 versions.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
238
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
239 * Avoid backslashes whenever possible. They make it more
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
240 difficult to edit code, and they are ugly (as well as potentially
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
241 dangerous if there are trailing white spaces).
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
242
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
243 .. code-block:: python
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
244
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
245 # Good.
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
246 if (cond_1 and
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
247 cond_2 and
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
248 cond_3):
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
249 ...
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
250 # Bad.
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
251 if cond_1 and \
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
252 cond_2 and \
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
253 cond_3:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
254 ...
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
255
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
256 * When indenting multi-line statements like lists or function arguments,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
257 keep elements of the same level aligned with each other.
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
258 The position of the first
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
259 element (on the same line or a new line) should be chosen depending on
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
260 what is easiest to read (sometimes both can be ok).
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
261
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
262 .. code-block:: python
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
263
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
264 # Good.
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
265 for my_very_long_variable_name in [my_foo, my_bar, my_love,
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
266 my_everything]:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
267 ...
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
268 for my_very_long_variable_name in [
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
269 my_foo, my_bar, my_love, my_everything]:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
270 ...
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
271 # Good iff the list needs to be frequently updated or is easier to
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
272 # understand when each element is on its own line.
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
273 for my_very_long_variable_name in [
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
274 my_foo,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
275 my_bar,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
276 my_love,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
277 my_everything,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
278 ]:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
279 ...
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
280 # Good as long as it does not require more than two lines.
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
281 for my_very_long_variable_name in [my_foo,
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
282 my_bar]:
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
283 ...
1145
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
284 # Bad.
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
285 for my_very_long_variable_name in [my_foo, my_bar, my_love,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
286 my_everything]:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
287 ...
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
288 for my_very_long_variable_name in [my_foo,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
289 my_bar,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
290 my_love,
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
291 my_everything]:
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
292 ...
d6d73a9f07b8 API_coding_style: Started to work on official guidelines
Olivier Delalleau <delallea@iro>
parents: 1143
diff changeset
293
1159
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
294 * Use the ``key`` argument instead of ``cmp`` when sorting (for Python 3
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
295 compatibility).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
296
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
297 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
298
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
299 # Good.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
300 my_list.sort(key=abs)
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
301 # Bad.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
302 my_list.sort(cmp=lambda x, y: cmp(abs(x), abs(y)))
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
303
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
304 * Whenever you read / write binary files, specify it in the mode ('rb' for
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
305 reading, 'wb' for writing). This is important for cross-platform and
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
306 Python 3 compatibility (e.g. when pickling / unpickling objects).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
307
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
308 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
309
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
310 # Good.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
311 cPickle.dump(obj, open('my_obj.pkl', 'wb', protocol=-1))
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
312 # Bad.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
313 cPickle.dump(obj, open('my_obj.pkl', 'w', protocol=-1))
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
314
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
315 * Avoid tuple parameter unpacking as it can lead to very ugly code when
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
316 converting to Python 3.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
317
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
318 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
319
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
320 # Good.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
321 def f(x, y_z):
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
322 y, z = y_z
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
323 ...
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
324 # Bad.
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
325 def f(x, (y, z)):
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
326 ...
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
327
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
328 * Only use ``cPickle``, not ``pickle`` (except for debugging purpose since
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
329 error messages from ``pickle`` are sometimes easier to understand).
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
330
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
331 * A script's only top-level code should be something like:
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
332
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
333 .. code-block:: python
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
334
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
335 if __name__ == '__main__':
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
336 sys.exit(main())
531e77fb67f2 coding_style: Moved more elements to official 'API'
Olivier Delalleau <delallea@iro>
parents: 1150
diff changeset
337
1150
d7192e52653e coding_style: Moved some elements to official API
Olivier Delalleau <delallea@iro>
parents: 1148
diff changeset
338
1147
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
339 The ``logging`` Module vs. the ``warning`` Module
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
340 =================================================
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
341
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
342 The ``logging`` Module
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
343 ----------------------
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
344
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
345 A central logging facility for Python capable of logging messages of various
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
346 categories/urgency and choosing with some granularity which messages are
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
347 displayed/suppressed, as well as where they are displayed or written. This
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
348 includes an ``INFO`` level for innocuous status information, a ``WARNING`` level
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
349 for unexpected state that is still recoverable, ``DEBUG`` for detailed
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
350 information which is only really of interest when things are going wrong, etc.
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
351
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
352 In addition to the `library documentation`_, see this helpful tutorial,
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
353 `Python Logging 101`_.
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
354
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
355 .. _library documentation: http://docs.python.org/library/logging.html
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
356 .. _Python Logging 101: http://plumberjack.blogspot.com/2009/09/python-logging-101.html
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
357
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
358 The ``warning`` Module
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
359 ----------------------
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
360
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
361 The ``warning`` module in the standard library and its main interface, the
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
362 ``warn()`` function, allows the programmer to issue warnings in situations where
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
363 they wish to alert the user to some condition, but the situation is not
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
364 urgent enough to throw an exception. By default, a warning issued at a given
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
365 line of the code will only be displayed the first time that line is executed.
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
366 By default, warnings are written to ``sys.stderr`` but the ``warning`` module
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
367 contains flexible facilities for altering the defaults, redirecting, etc.
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
368
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
369 Which? When?
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
370 ------------
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
371
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
372 It is our feeling that the ``logging`` module's ``WARNING`` level be used to log
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
373 warnings more meant for *internal*, *developer* consumption, to log situations
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
374 where something unexpected happened that may be indicative of a problem but
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
375 is several layers of abstraction below what a user of the library would
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
376 care about.
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
377
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
378 By contrast, the warning module should be used for warnings intended for user
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
379 consumption, e.g. alerting them that their version of Pylearn is older than
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
380 this plugin requires, so things may not work as expected, or that a given
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
381 function/class/method is slated for deprecation in a coming release (early
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
382 in the library's lifetime, ``DeprecationWarning`` will likely be the most common
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
383 case). The warning message issued through this facility should avoid
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
384 referring to Pylearn internals.
f6011a2aff0b coding_style: Moved David's comments from coding_style.txt to API_coding_style.txt
Olivier Delalleau <delallea@iro>
parents: 1145
diff changeset
385
1148
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
386 Code Sample
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
387 ===========
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
388
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
389 The following code sample illustrates many of the coding guidelines one should
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
390 follow in Pylearn.
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
391
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
392 .. code-block:: python
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
393
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
394 import os, sys, time
2da593b0f29d API_coding_style: Moved code sample at end of document for better readability
Olivier Delalleau <delallea@iro>
parents: 1147
diff changeset
395