summaryrefslogtreecommitdiff
blob: 725f1ae1eabbe5d3e28f8f4268ce77bb1cbc9cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Code Guidelines
---------------
A few code guidelines to try to stick to, please comment of none of these make
sense, they are pretty basic and mostly apply to old code.  However for people
who are looking at current code, they make take up bad habits that exist in the
current codebase.

Strings
-------
Try not to use the functions in the string module, they are deprecated.

string.join(<iterable>," ")

should be replaced with:

" ".join(<iterable>)

and:

string.split(string, delimeter)

should be replaced with:

string.split(delimeter)

Nearly all other methods in string work on string objects and have similar calling
conventions.

Comparisons
-----------

if foo == None

should be replaced with:

if foo is not None:

Is not does a reference comparison (address1 = address2 basically) and 
the == forces a by value compare (with __eq__())

Dict Lookups
------------

Try not to use has_key, you can use

if foo in dict

instead of if dict.has_key(foo)

Also don't do stuff like:

if foo in dict and dict[foo]:

Generally you can do two things here, if you are messing with defaults..

dict.get(foo, some_default)

will try to retrieve foo from dict, if there is a KeyError, will insert foo
into dict with the value of some_default.  This method is preferred in cases where
you are messing with defaults:

try:
	dict[foo]
except KeyError:
	dict[foo] = default_value

The get call is nicer (compact) and faster (try,except are slow).

Imports
-------

Import things one per line

YES:
  import os
  import time
  import sys

NO:
  import os,sys,time

When importing from a module, you may import more than 1 thing at a time.

YES:
  from portage.module import foo, bar, baz

Multiline imports are ok (for now :))

Try to group system and package imports separately.

YES:
  import os
  import sys
  import time

  from portage.locks import lockfile
  from portage.versions import vercmp

NO:
  import os
  import portage
  import portage.util
  import time
  import sys