TWiki
>
UCSDTier2 Web
>
GlideinWMS
>
FormattingGuideLines
(2010/08/13,
ParagMhashilkar
)
(raw view)
E
dit
A
ttach
---+ Formatting the Code * Use standard libraries as much as possible * Try avoiding the deprecated API's * Avoid using tabs. Use spaces for indentation. * Standard python code has 4 spaces for indentation, so use it. * When in doubt always check with * http://www.python.org/dev/peps/pep-0008/ * http://www.python.org/dev/peps/pep-0257 ---+ Tips in a Nutshell * Use 4 spaces per indentation level.Never mix tabs and spaces. * Maximum Line Length should be preferably set to 79 or 80 characters * Separate top-level function and class definitions with two blank lines. * Method definitions inside a class are separated by a single blank line. * Extra blank lines may be used (sparingly) to separate groups of related functions. * Code in the core Python distribution should aways use the ASCII or Latin-1 encoding (a.k.a. ISO-8859-1). For Python 3.0 and beyond, UTF-8 is preferred over Latin-1 * Imports should usually be on separate lines rather than comma separated. * Comments that contradict the code are worse than no comments. * Comments should be complete sentences. * Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. * Use inline comments sparingly. * Follow sections in link above for Documentation Strings, Version Bookkeeping, Naming Conventions * Avoid extraneous whitespace in the following situations: <table align="center" cellspacing="0" cellpadding="0" border="1"> <tbody> <tr> <th>Situation</th> <th>Recommended Way</th> <th>Not Recommended</th> </tr> <tr> <td>Immediately inside parentheses, brackets or braces.</td> <td><verbatim>spam(ham[1], {eggs: 2}) </verbatim></td> <td> <verbatim>spam( ham[ 1 ], { eggs: 2 } ) </verbatim></td> </tr> <tr> <td>Immediately before a comma, semicolon, or colon.</td> <td><verbatim>if x == 4: print x, y; x, y = y, x </verbatim></td> <td><verbatim>if x == 4 : print x , y ; x , y = y , x </verbatim></td> </tr> <tr> <td>Immediately before the open parenthesis that starts the argument list of a function call.</td> <td><verbatim>spam(1)</verbatim></td> <td><verbatim>spam (1)</verbatim></td> </tr> <tr> <td>Immediately before the open parenthesis that starts an indexing or slicing</td> <td><verbatim>dict['key'] = list[index]</verbatim></td> <td><verbatim>dict ['key'] = list [index]</verbatim></td> </tr> <tr> <td>More than one space around an assignment (or other) operator to align it with another</td> <td><verbatim> x = 1 long_variable = 3</verbatim></td> <td><verbatim> x = 1 long_variable = 3</verbatim></td> </tr> <tr> <td>Always surround these binary operators with a single space on either side: assignment ( =), augmented assignment (+=, -= etc.), comparisons ( ==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not)</pre></td> <td> <verbatim> i = i + 1 submitted += 1 x = x * 2 - 1 hypot2 = x * x + y * y c = (a + b) * (a - b)</verbatim></td> <td><verbatim> i=i+1 submitted +=1 x = x*2 - 1 hypot2 = x*x + y*y c = (a+b) * (a-b)</verbatim></td> </tr> <tr> <td>Don't use spaces around the '=' sign when used to indicate a keyword argument or a default parameter value.</td> <td><verbatim> def complex(real, imag=0.0): return magic(r=real, i=imag)</verbatim></td> <td><verbatim> def complex(real, imag = 0.0): return magic(r = real, i = imag)</verbatim></td> </tr> <tr> <td>Compound statements (multiple statements on the same line) are generally discouraged.</td> <td><verbatim> if foo == 'blah': do_blah_thing() do_one() do_two() do_three()</verbatim></td> <td><verbatim> if foo == 'blah': do_blah_thing() do_one(); do_two(); do_three()</verbatim></td> </tr> <tr> <td>While sometimes it's okay to put an if/for/while with a small body on the same line, never do this for multi-clause statements. Also avoid folding such long lines!</td> <td>-</td> <td><verbatim>if foo == 'blah': do_blah_thing() else: do_non_blah_thing() try: something() finally: cleanup() do_one(); do_two(); do_three(long, argument, list, like, this) if foo == 'blah': one(); two(); three()</verbatim></td> </tr> </tbody> </table>
E
dit
|
A
ttach
|
P
rint version
|
H
istory
: r1
|
B
acklinks
|
V
iew topic
|
Ra
w
edit
|
M
ore topic actions
Topic revision: r1 - 2010/08/13 - 16:53:31 -
ParagMhashilkar
UCSDTier2
Log In
UCSDTier2 Web
Create New Topic
Index
Search
Changes
Notifications
Statistics
Preferences
Webs
CMSBrownBag
CMSUCSD
HEPProjects
Main
Sandbox
TWiki
UCLHCWeb
UCSDHepBrownBag
UCSDScaleTests
UCSDTier2
USCMSWeb
Copyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki?
Send feedback