Remove pred from compat.any/all
authorGuido Trotter <ultrotter@google.com>
Wed, 23 Jun 2010 22:17:42 +0000 (00:17 +0200)
committerGuido Trotter <ultrotter@google.com>
Mon, 28 Jun 2010 11:04:20 +0000 (12:04 +0100)
This makes it compatible with the python builtin, and we can even use
the builtin when running under the right version of python. The all and
any functions are renamed to _all and _any, so that they can be tested,
and (non)existing unittests are updated (translation: there are no unit
tests, so none are updated).

This patch also moves the code deciding which partial to use next to
the definition of the _partial function, rather than after TryToRoman.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

lib/bdev.py
lib/cmdlib.py
lib/compat.py
lib/masterd/instance.py
scripts/gnt-cluster
tools/burnin

index f377f16..f2b96f4 100644 (file)
@@ -389,7 +389,7 @@ class LogicalVolume(BlockDev):
     pvs_info.reverse()
 
     pvlist = [ pv[1] for pv in pvs_info ]
-    if compat.any(pvlist, lambda v: ":" in v):
+    if compat.any(":" in v for v in pvlist):
       _ThrowError("Some of your PVs have the invalid character ':' in their"
                   " name, this is not supported - please filter them out"
                   " in lvm.conf using either 'filter' or 'preferred_names'")
@@ -463,7 +463,7 @@ class LogicalVolume(BlockDev):
     """
     if (not cls._VALID_NAME_RE.match(name) or
         name in cls._INVALID_NAMES or
-        compat.any(cls._INVALID_SUBSTRINGS, lambda x: x in name)):
+        compat.any(substring in name for substring in cls._INVALID_SUBSTRINGS)):
       _ThrowError("Invalid LVM name '%s'", name)
 
   def Remove(self):
index caea319..a991186 100644 (file)
@@ -176,7 +176,7 @@ def _TListOf(my_type):
 
   """
   return _TAnd(_TList,
-               lambda lst: compat.all(lst, my_type))
+               lambda lst: compat.all(my_type(v) for v in lst))
 
 
 def _TDictOf(key_type, val_type):
@@ -184,8 +184,9 @@ def _TDictOf(key_type, val_type):
 
   """
   return _TAnd(_TDict,
-               lambda my_dict: (compat.all(my_dict.keys(), key_type) and
-                                compat.all(my_dict.values(), val_type)))
+               lambda my_dict: (compat.all(key_type(v) for v in my_dict.keys())
+                                and compat.all(val_type(v)
+                                               for v in my_dict.values())))
 
 
 # End types
@@ -1744,8 +1745,8 @@ class LUVerifyCluster(LogicalUnit):
 
     remote_os = nresult.get(constants.NV_OSLIST, None)
     test = (not isinstance(remote_os, list) or
-            not compat.all(remote_os,
-                           lambda v: isinstance(v, list) and len(v) == 7))
+            not compat.all(isinstance(v, list) and len(v) == 7
+                           for v in remote_os))
 
     _ErrorIf(test, self.ENODEOS, node,
              "node hasn't returned valid OS data")
@@ -1794,7 +1795,7 @@ class LUVerifyCluster(LogicalUnit):
                "OS '%s' has multiple entries (first one shadows the rest): %s",
                os_name, utils.CommaJoin([v[0] for v in os_data]))
       # this will catched in backend too
-      _ErrorIf(compat.any(f_api, lambda v: v >= constants.OS_API_V15)
+      _ErrorIf(compat.any(v >= constants.OS_API_V15 for v in f_api)
                and not f_var, self.ENODEOS, node,
                "OS %s with API at least %d does not declare any variant",
                os_name, constants.OS_API_V15)
index 51ff904..ca656ed 100644 (file)
@@ -55,29 +55,31 @@ except ImportError:
   sha1_hash = sha.new
 
 
-def all(seq, pred=bool): # pylint: disable-msg=W0622
-  """Returns True if pred(x) is True for every element in the iterable.
-
-  Please note that this function provides a C{pred} parameter which isn't
-  available in the version included in Python 2.5 and above.
+def _all(seq):
+  """Returns True if all elements in the iterable are True.
 
   """
-  for _ in itertools.ifilterfalse(pred, seq):
+  for _ in itertools.ifilterfalse(bool, seq):
     return False
   return True
 
-
-def any(seq, pred=bool): # pylint: disable-msg=W0622
-  """Returns True if pred(x) is True for at least one element in the iterable.
-
-  Please note that this function provides a C{pred} parameter which isn't
-  available in the version included in Python 2.5 and above.
+def _any(seq):
+  """Returns True if any element of the iterable are True.
 
   """
-  for _ in itertools.ifilter(pred, seq):
+  for _ in itertools.ifilter(bool, seq):
     return True
   return False
 
+try:
+  all = all # pylint: disable-msg=W0622
+except NameError:
+  all = _all
+
+try:
+  any = any # pylint: disable-msg=W0622
+except NameError:
+  any = _any
 
 def partition(seq, pred=bool): # pylint: disable-msg=W0622
   """Partition a list in two, based on the given predicate.
@@ -106,6 +108,12 @@ def _partial(func, *args, **keywords): # pylint: disable-msg=W0622
   return newfunc
 
 
+if functools is None:
+  partial = _partial
+else:
+  partial = functools.partial
+
+
 def TryToRoman(val, convert=True):
   """Try to convert a value to roman numerals
 
@@ -128,8 +136,3 @@ def TryToRoman(val, convert=True):
   else:
     return val
 
-
-if functools is None:
-  partial = _partial
-else:
-  partial = functools.partial
index 490ffce..eb44b58 100644 (file)
@@ -793,7 +793,7 @@ class ImportExportLoop:
           logging.exception("%s failed", diskie.MODE_TEXT)
           diskie.Finalize(error=str(err))
 
-      if not compat.any([diskie.active for diskie in self._queue]):
+      if not compat.any(diskie.active for diskie in self._queue):
         break
 
       # Wait a bit
@@ -1060,11 +1060,11 @@ def TransferInstanceData(lu, feedback_fn, src_node, dest_node, dest_ip,
     ieloop.FinalizeAll()
 
   assert len(all_dtp) == len(all_transfers)
-  assert compat.all([(dtp.src_export is None or
+  assert compat.all((dtp.src_export is None or
                       dtp.src_export.success is not None) and
                      (dtp.dest_import is None or
                       dtp.dest_import.success is not None)
-                     for dtp in all_dtp]), \
+                     for dtp in all_dtp), \
          "Not all imports/exports are finalized"
 
   return [bool(dtp.success) for dtp in all_dtp]
index da982ce..e4a5680 100755 (executable)
@@ -433,7 +433,7 @@ def VerifyDisks(opts, args):
 
   if missing:
     for iname, ival in missing.iteritems():
-      all_missing = compat.all(ival, lambda x: x[0] in bad_nodes)
+      all_missing = compat.all(x[0] in bad_nodes for x in ival)
       if all_missing:
         ToStdout("Instance %s cannot be verified as it lives on"
                  " broken nodes", iname)
index e7da37d..9206db5 100755 (executable)
@@ -987,7 +987,7 @@ class Burner(object):
         self.BurnReplaceDisks2()
 
       if (opts.disk_template in constants.DTS_GROWABLE and
-          compat.any(self.disk_growth, lambda n: n > 0)):
+          compat.any(n > 0 for n in self.disk_growth)):
         self.BurnGrowDisks()
 
       if opts.do_failover and opts.disk_template in constants.DTS_NET_MIRROR: