1 |
Attached is a buttload of patches cleaning modules for the following |
2 |
things- |
3 |
|
4 |
1) types module usage. Use isinstance instead. |
5 |
2) string module usage. Use string methods instead. |
6 |
3) stat module usage to get attributes. Stat objects have named |
7 |
attributes, use them (example would be st[stats.ST_MODE] vs |
8 |
st.st_mode) |
9 |
4) range usage. Use xrange instead (former returns a list, latter is |
10 |
a generator, eg, less mem usage and usually faster) |
11 |
5) testing for a key, if it doesn't exist, setting a default. Use |
12 |
setdefault method instead. |
13 |
6) testing for a key, if it exists, assign that to a var, else assign |
14 |
a default to the var. use get method instead. |
15 |
7) ==/!= None tests. use is/is not None instead (faster, ptr |
16 |
comparison instead of going through the equality protocol). |
17 |
8) len testing. |
18 |
if len(blah): # actually is if bool(len(blah)) effectively |
19 |
use |
20 |
if blah: |
21 |
instead- the object knows it's being evaluated being evaluated in a |
22 |
boolean context, thus avoids intermediate steps. |
23 |
9) repeated regex compilation. If it's a constant pattern, compile it |
24 |
once. |
25 |
10) lack of awareness of iterating over dicts directly, and of |
26 |
iteritems(). Conversion of for x in d.keys() -> to for x in d: |
27 |
Latter doesn't create an intermediate list, eg faster/cheaper mem |
28 |
wise (note to anyone paying attention, only iterate over the dict |
29 |
directly if you're not mutating the dict within the loop). |
30 |
Final note, if you're doing 'if x in d.keys()', you need to be |
31 |
back handed, forcing a linear search for no reason. |
32 |
11) imports cleanup. If it doesn't use it, no point in importing. |
33 |
12) General exception cleanup. Know what can be thrown, and catch |
34 |
that instead of catching everything and requiring SystemExit crap. |
35 |
13) Set exists, and python 2.3 is already forced. Use it. :) |
36 |
Related note, portage_util forces sets.Set to be used which is stupid |
37 |
if set type exists (former is implemented in python, latter is |
38 |
implemented in cpython, ie, there is a difference in speed). |
39 |
14) Strings are immutable. They can't be changed, as such attempting to |
40 |
copy them is stupid (python just returns the string, but still, |
41 |
don't do it). |
42 |
15) Bundling your own version of spawn is Plain Idiotic (TM). |
43 |
16) logic cleanup/simplification. |
44 |
|
45 |
Modules involved here are mostly straightforward cleanup- changes |
46 |
should be obvious (sans getbinpkg). |
47 |
|
48 |
Largest set of changes really is in getbinpkg- code in there is quite |
49 |
crufty, and a bit dense (IOW, check that chunk over carefully please). |
50 |
|
51 |
With the potential exception of getbinpkg, these particular patches |
52 |
are all potential 2.1 material imo; no huge rush on that however |
53 |
considering that the code already works (these patches just makes the |
54 |
code suck less). |
55 |
|
56 |
At the very least, include the portage_util set change; it'll result |
57 |
in a faster unique_array. |
58 |
|
59 |
Either way, second set of eyes on the changes would be useful... |
60 |
|
61 |
~harring |