Do not use list comprehensions with any or all

python any and all are able to break early the moment they come across a
member of the iterable that meets the condition; but having a list
comprehension nested within breaks that ability to exit early, as the
list comprehension has to finish being constructed first before it can
be evaluated
This commit is contained in:
Ogi Moore 2021-04-20 22:25:23 -07:00
parent 314121192a
commit 3ea92736b8
4 changed files with 5 additions and 5 deletions

View File

@ -202,7 +202,7 @@ def findRefPath(startObj, endObj, maxLen=8, restart=True, seen={}, path=None, ig
#print prefix+" FRAME"
continue
try:
if any([r is x for x in path]):
if any(r is x for x in path):
#print prefix+" LOOP", objChainString([r]+path)
continue
except:
@ -282,7 +282,7 @@ def refPathString(chain):
o2 = chain[i]
cont = False
if isinstance(o1, list) or isinstance(o1, tuple):
if any([o2 is x for x in o1]):
if any(o2 is x for x in o1):
s += "[%d]" % o1.index(o2)
continue
#print " not list"

View File

@ -126,7 +126,7 @@ class SplitContainer(Container, QtGui.QSplitter):
def saveState(self):
sizes = self.sizes()
if all([x == 0 for x in sizes]):
if all(x == 0 for x in sizes):
sizes = [10] * len(sizes)
return {'sizes': sizes}

View File

@ -274,7 +274,7 @@ class ImageView(QtGui.QWidget):
if not isinstance(img, np.ndarray):
required = ['dtype', 'max', 'min', 'ndim', 'shape', 'size']
if not all([hasattr(img, attr) for attr in required]):
if not all(hasattr(img, attr) for attr in required):
raise TypeError("Image must be NumPy array or any object "
"that provides compatible attributes/methods:\n"
" %s" % str(required))

View File

@ -124,7 +124,7 @@ class MetaArray(object):
nameTypes = [basestring, tuple]
@staticmethod
def isNameType(var):
return any([isinstance(var, t) for t in MetaArray.nameTypes])
return any(isinstance(var, t) for t in MetaArray.nameTypes)
## methods to wrap from embedded ndarray / HDF5