1212import os
1313import stat
1414import warnings
15+ from itertools import ifilter , ifilterfalse
1516
1617__all__ = ["cmp" ,"dircmp" ,"cmpfiles" ]
1718
@@ -69,13 +70,13 @@ def _do_cmp(f1, f2):
6970 bufsize = BUFSIZE
7071 fp1 = open (f1 , 'rb' )
7172 fp2 = open (f2 , 'rb' )
72- while 1 :
73+ while True :
7374 b1 = fp1 .read (bufsize )
7475 b2 = fp2 .read (bufsize )
7576 if b1 != b2 :
76- return 0
77+ return False
7778 if not b1 :
78- return 1
79+ return True
7980
8081# Directory comparison class.
8182#
@@ -133,46 +134,12 @@ def phase0(self): # Compare everything except common subdirectories
133134 self .left_list .sort ()
134135 self .right_list .sort ()
135136
136- __p4_attrs = ('subdirs' ,)
137- __p3_attrs = ('same_files' , 'diff_files' , 'funny_files' )
138- __p2_attrs = ('common_dirs' , 'common_files' , 'common_funny' )
139- __p1_attrs = ('common' , 'left_only' , 'right_only' )
140- __p0_attrs = ('left_list' , 'right_list' )
141-
142- def __getattr__ (self , attr ):
143- if attr in self .__p4_attrs :
144- self .phase4 ()
145- elif attr in self .__p3_attrs :
146- self .phase3 ()
147- elif attr in self .__p2_attrs :
148- self .phase2 ()
149- elif attr in self .__p1_attrs :
150- self .phase1 ()
151- elif attr in self .__p0_attrs :
152- self .phase0 ()
153- else :
154- raise AttributeError , attr
155- return getattr (self , attr )
156-
157137 def phase1 (self ): # Compute common names
158- a_only , b_only = [], []
159- common = {}
160- b = {}
161- for fnm in self .right_list :
162- b [fnm ] = 1
163- for x in self .left_list :
164- if b .get (x , 0 ):
165- common [x ] = 1
166- else :
167- a_only .append (x )
168- for x in self .right_list :
169- if common .get (x , 0 ):
170- pass
171- else :
172- b_only .append (x )
138+ b = dict .fromkeys (self .right_list )
139+ common = dict .fromkeys (ifilter (b .has_key , self .left_list ))
140+ self .left_only = list (ifilterfalse (common .has_key , self .left_list ))
141+ self .right_only = list (ifilterfalse (common .has_key , self .right_list ))
173142 self .common = common .keys ()
174- self .left_only = a_only
175- self .right_only = b_only
176143
177144 def phase2 (self ): # Distinguish files, directories, funnies
178145 self .common_dirs = []
@@ -265,6 +232,17 @@ def report_full_closure(self): # Report on self and subdirs recursively
265232 print
266233 sd .report_full_closure ()
267234
235+ methodmap = dict (subdirs = phase4 ,
236+ same_files = phase3 , diff_files = phase3 , funny_files = phase3 ,
237+ common_dirs = phase2 , common_files = phase2 , common_funny = phase2 ,
238+ common = phase1 , left_only = phase1 , right_only = phase1 ,
239+ left_list = phase0 , right_list = phase0 )
240+
241+ def __getattr__ (self , attr ):
242+ if attr not in self .methodmap :
243+ raise AttributeError , attr
244+ self .methodmap [attr ](self )
245+ return getattr (self , attr )
268246
269247def cmpfiles (a , b , common , shallow = 1 , use_statcache = None ):
270248 """Compare common files in two directories.
@@ -297,7 +275,7 @@ def cmpfiles(a, b, common, shallow=1, use_statcache=None):
297275# 1 for different
298276# 2 for funny cases (can't stat, etc.)
299277#
300- def _cmp (a , b , sh ):
278+ def _cmp (a , b , sh , abs = abs , cmp = cmp ):
301279 try :
302280 return not abs (cmp (a , b , sh ))
303281 except os .error :
@@ -306,11 +284,8 @@ def _cmp(a, b, sh):
306284
307285# Return a copy with items that occur in skip removed.
308286#
309- def _filter (list , skip ):
310- result = []
311- for item in list :
312- if item not in skip : result .append (item )
313- return result
287+ def _filter (flist , skip ):
288+ return list (ifilterfalse (skip .__contains__ , flist ))
314289
315290
316291# Demonstration and testing.
0 commit comments