[Python-checkins] python/dist/src/Lib filecmp.py,1.15,1.16

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Wed, 26 Feb 2003 16:05:37 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv10274

Modified Files:
	filecmp.py 
Log Message:
Module review:
* Changed variable name from 'list' to 'flist'.
* Replaced "while 1" with "while True".
* Replaced if/elif/elif/elif structure with a shorter and
  faster dispatch dictionary that maps attrs to methods.
* Simplified and sped comparison logic by using
  ifilter, ifilterfalse, and dict.fromkeys.
* Used True and False rather than 1 and 0.



Index: filecmp.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/filecmp.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** filecmp.py	6 Feb 2003 19:38:45 -0000	1.15
--- filecmp.py	27 Feb 2003 00:05:31 -0000	1.16
***************
*** 13,16 ****
--- 13,17 ----
  import stat
  import warnings
+ from itertools import ifilter, ifilterfalse
  
  __all__ = ["cmp","dircmp","cmpfiles"]
***************
*** 70,80 ****
      fp1 = open(f1, 'rb')
      fp2 = open(f2, 'rb')
!     while 1:
          b1 = fp1.read(bufsize)
          b2 = fp2.read(bufsize)
          if b1 != b2:
!             return 0
          if not b1:
!             return 1
  
  # Directory comparison class.
--- 71,81 ----
      fp1 = open(f1, 'rb')
      fp2 = open(f2, 'rb')
!     while True:
          b1 = fp1.read(bufsize)
          b2 = fp2.read(bufsize)
          if b1 != b2:
!             return False
          if not b1:
!             return True
  
  # Directory comparison class.
***************
*** 134,177 ****
          self.right_list.sort()
  
-     __p4_attrs = ('subdirs',)
-     __p3_attrs = ('same_files', 'diff_files', 'funny_files')
-     __p2_attrs = ('common_dirs', 'common_files', 'common_funny')
-     __p1_attrs = ('common', 'left_only', 'right_only')
-     __p0_attrs = ('left_list', 'right_list')
- 
-     def __getattr__(self, attr):
-         if attr in self.__p4_attrs:
-             self.phase4()
-         elif attr in self.__p3_attrs:
-             self.phase3()
-         elif attr in self.__p2_attrs:
-             self.phase2()
-         elif attr in self.__p1_attrs:
-             self.phase1()
-         elif attr in self.__p0_attrs:
-             self.phase0()
-         else:
-             raise AttributeError, attr
-         return getattr(self, attr)
- 
      def phase1(self): # Compute common names
!         a_only, b_only = [], []
!         common = {}
!         b = {}
!         for fnm in self.right_list:
!             b[fnm] = 1
!         for x in self.left_list:
!             if b.get(x, 0):
!                 common[x] = 1
!             else:
!                 a_only.append(x)
!         for x in self.right_list:
!             if common.get(x, 0):
!                 pass
!             else:
!                 b_only.append(x)
          self.common = common.keys()
-         self.left_only = a_only
-         self.right_only = b_only
  
      def phase2(self): # Distinguish files, directories, funnies
--- 135,144 ----
          self.right_list.sort()
  
      def phase1(self): # Compute common names
!         b = dict.fromkeys(self.right_list)
!         common = dict.fromkeys(ifilter(b.has_key, self.left_list))
!         self.left_only = list(ifilterfalse(common.has_key, self.left_list))
!         self.right_only = list(ifilterfalse(common.has_key, self.right_list))
          self.common = common.keys()
  
      def phase2(self): # Distinguish files, directories, funnies
***************
*** 266,269 ****
--- 233,247 ----
              sd.report_full_closure()
  
+     methodmap = dict(subdirs=phase4,
+                      same_files=phase3, diff_files=phase3, funny_files=phase3,
+                      common_dirs = phase2, common_files=phase2, common_funny=phase2,
+                      common=phase1, left_only=phase1, right_only=phase1,
+                      left_list=phase0, right_list=phase0)
+ 
+     def __getattr__(self, attr):
+         if attr not in self.methodmap:
+             raise AttributeError, attr
+         self.methodmap[attr](self)
+         return getattr(self, attr)
  
  def cmpfiles(a, b, common, shallow=1, use_statcache=None):
***************
*** 298,302 ****
  #       2 for funny cases (can't stat, etc.)
  #
! def _cmp(a, b, sh):
      try:
          return not abs(cmp(a, b, sh))
--- 276,280 ----
  #       2 for funny cases (can't stat, etc.)
  #
! def _cmp(a, b, sh, abs=abs, cmp=cmp):
      try:
          return not abs(cmp(a, b, sh))
***************
*** 307,315 ****
  # Return a copy with items that occur in skip removed.
  #
! def _filter(list, skip):
!     result = []
!     for item in list:
!         if item not in skip: result.append(item)
!     return result
  
  
--- 285,290 ----
  # Return a copy with items that occur in skip removed.
  #
! def _filter(flist, skip):
!     return list(ifilterfalse(skip.__contains__, flist))