Changes between Version 4 and Version 5 of PythonPort/Design


Ignore:
Timestamp:
Oct 15, 2007, 3:24:23 PM (18 years ago)
Author:
Daniel Wallin
Comment:

Python code style guidelines

Legend:

Unmodified
Added
Removed
Modified
  • PythonPort/Design

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