| 21 | | |
| | 21 | |
| | 22 | == Python style == |
| | 23 | |
| | 24 | Please try to adhere to the Python style guidelines at: |
| | 25 | http://www.python.org/dev/peps/pep-0008/. |
| | 26 | |
| | 27 | The current code base breaks against a lot of these rules. If you encounter any |
| | 28 | of these problems while working on the code, please fix them as you go. |
| | 29 | |
| | 30 | The most common problems are extraneous whitespace. |
| | 31 | |
| | 32 | '''NO''' |
| | 33 | {{{ |
| | 34 | __declared_subfeature [str (t, name)] = True |
| | 35 | m = __re__before_first_dash.match (toolset) |
| | 36 | }}} |
| | 37 | |
| | 38 | '''YES''' |
| | 39 | {{{ |
| | 40 | __declared_subfeature[str(t, name)] = True |
| | 41 | m = __re__before_first_dash.match(toolset) |
| | 42 | }}} |
| | 43 | |
| | 44 | This is bad style. No whitespace should be used before brackets or parentheses. |
| | 45 | Sometimes whitespace is also used inside braces: |
| | 46 | |
| | 47 | '''NO''' |
| | 48 | {{{ |
| | 49 | { 'foo': 'bar' } |
| | 50 | }}} |
| | 51 | |
| | 52 | '''YES''' |
| | 53 | {{{ |
| | 54 | {'foo': 'bar'} |
| | 55 | }}} |
| | 56 | |
| | 57 | These whitespace should also be removed. Whitespace should also not be present |
| | 58 | in argument defaults: |
| | 59 | |
| | 60 | '''NO''' |
| | 61 | {{{ |
| | 62 | def __init__(self, name, project, manager = None): |
| | 63 | }}} |
| | 64 | |
| | 65 | '''YES''' |
| | 66 | {{{ |
| | 67 | def __init__(self, name, project, manager=None): |
| | 68 | }}} |
| | 69 | |
| | 70 | Another common issue is using `has_key` to determine if a key is present |
| | 71 | in a dictionary. Instead operator `in` should be used: |
| | 72 | |
| | 73 | '''NO''' |
| | 74 | {{{ |
| | 75 | if foo.has_key('bar') |
| | 76 | }}} |
| | 77 | |
| | 78 | '''YES''' |
| | 79 | {{{ |
| | 80 | if 'bar' in foo: |
| | 81 | }}} |
| | 82 | |
| | 83 | Don't use [] as an argument default without making absolutely sure |
| | 84 | the argument isn't mutated inside the function. Instead, use something |
| | 85 | like: |
| | 86 | |
| | 87 | {{{ |
| | 88 | def foo(x=None): |
| | 89 | if x is None: |
| | 90 | x = [] |
| | 91 | }}} |
| | 92 | |
| | 93 | Don't use `len()` to test for empty sequences: |
| | 94 | |
| | 95 | '''NO''' |
| | 96 | {{{ |
| | 97 | if len(x): |
| | 98 | if not len(x): |
| | 99 | }}} |
| | 100 | |
| | 101 | '''YES''' |
| | 102 | {{{ |
| | 103 | if x: |
| | 104 | if not x: |
| | 105 | }}} |
| | 106 | |
| | 107 | |