Skip to content
This repository
Browse code

REF: Replace filters with list comprehensions.

2to3 does a lot of list(filter(...)) sort of thing which can be
avoided by using list comprehensions instead of filters. This
also seems to clarify the code to a considerable degree.
  • Loading branch information...
commit 9a73697c70e667c4655a01d3f76e5a9e850f1798 1 parent b990ed5
Charles Harris charris authored
2  numpy/distutils/command/build_py.py
@@ -21,7 +21,7 @@ def find_package_modules(self, package, package_dir):
21 21
22 22 def find_modules(self):
23 23 old_py_modules = self.py_modules[:]
24   - new_py_modules = list(filter(is_string, self.py_modules))
  24 + new_py_modules = [_m for _m in self.py_modules if is_string(_m)]
25 25 self.py_modules[:] = new_py_modules
26 26 modules = old_build_py.find_modules(self)
27 27 self.py_modules[:] = old_py_modules
8 numpy/distutils/misc_util.py
@@ -555,7 +555,7 @@ def general_source_directories_files(top_path):
555 555 def get_ext_source_files(ext):
556 556 # Get sources and any include files in the same directory.
557 557 filenames = []
558   - sources = list(filter(is_string, ext.sources))
  558 + sources = [_m for _m in ext.sources if is_string(_m)]
559 559 filenames.extend(sources)
560 560 filenames.extend(get_dependencies(sources))
561 561 for d in ext.depends:
@@ -566,13 +566,13 @@ def get_ext_source_files(ext):
566 566 return filenames
567 567
568 568 def get_script_files(scripts):
569   - scripts = list(filter(is_string, scripts))
  569 + scripts = [_m for _m in scripts if is_string(_m)]
570 570 return scripts
571 571
572 572 def get_lib_source_files(lib):
573 573 filenames = []
574 574 sources = lib[1].get('sources',[])
575   - sources = list(filter(is_string, sources))
  575 + sources = [_m for _m in sources if is_string(_m)]
576 576 filenames.extend(sources)
577 577 filenames.extend(get_dependencies(sources))
578 578 depends = lib[1].get('depends',[])
@@ -808,7 +808,7 @@ def _wildcard_get_subpackage(self, subpackage_name,
808 808 caller_level = 1):
809 809 l = subpackage_name.split('.')
810 810 subpackage_path = njoin([self.local_path]+l)
811   - dirs = list(filter(os.path.isdir,glob.glob(subpackage_path)))
  811 + dirs = [_m for _m in glob.glob(subpackage_path) if os.path.isdir(_m)]
812 812 config_list = []
813 813 for d in dirs:
814 814 if not os.path.isfile(njoin(d,'__init__.py')):
6 numpy/distutils/system_info.py
@@ -232,9 +232,9 @@ def libpaths(paths, bits):
232 232 default_include_dirs.append(os.path.join(sys.prefix, 'include'))
233 233 default_src_dirs.append(os.path.join(sys.prefix, 'src'))
234 234
235   -default_lib_dirs = list(filter(os.path.isdir, default_lib_dirs))
236   -default_include_dirs = list(filter(os.path.isdir, default_include_dirs))
237   -default_src_dirs = list(filter(os.path.isdir, default_src_dirs))
  235 +default_lib_dirs = [_m for _m in default_lib_dirs if os.path.isdir(_m)]
  236 +default_include_dirs = [_m for _m in default_include_dirs if os.path.isdir(_m)]
  237 +default_src_dirs = [_m for _m in default_src_dirs if os.path.isdir(_m)]
238 238
239 239 so_ext = get_shared_lib_extension()
240 240
29 numpy/f2py/f2py2e.py
@@ -430,13 +430,14 @@ def run_compile():
430 430 remove_build_dir = 1
431 431 build_dir = os.path.join(tempfile.mktemp())
432 432
433   - sysinfo_flags = list(filter(re.compile(r'[-][-]link[-]').match,sys.argv[1:]))
434   - sys.argv = list(filter(lambda a,flags=sysinfo_flags:a not in flags,sys.argv))
  433 + sysinfo_flags = [_m for _m in sys.argv[1] if re.compile(r'[-][-]link[-]').match(_m)]
  434 + sys.argv = [_m for _m in sys.argv if not _m in sysinfo_flags]
435 435 if sysinfo_flags:
436 436 sysinfo_flags = [f[7:] for f in sysinfo_flags]
437 437
438   - f2py_flags = list(filter(re.compile(r'[-][-]((no[-]|)(wrap[-]functions|lower)|debug[-]capi|quiet)|[-]include').match,sys.argv[1:]))
439   - sys.argv = list(filter(lambda a,flags=f2py_flags:a not in flags,sys.argv))
  438 + f2py_flags = [_m for _m in sys.argv[1:] if
  439 + re.compile(r'[-][-]((no[-]|)(wrap[-]functions|lower)|debug[-]capi|quiet)|[-]include').match(_m)]
  440 + sys.argv = [_m for _m in sys.argv if not _m in f2py_flags]
440 441 f2py_flags2 = []
441 442 fl = 0
442 443 for a in sys.argv[1:]:
@@ -450,12 +451,13 @@ def run_compile():
450 451 f2py_flags2.append(':')
451 452 f2py_flags.extend(f2py_flags2)
452 453
453   - sys.argv = list(filter(lambda a,flags=f2py_flags2:a not in flags,sys.argv))
454   -
455   - flib_flags = list(filter(re.compile(r'[-][-]((f(90)?compiler([-]exec|)|compiler)=|help[-]compiler)').match,sys.argv[1:]))
456   - sys.argv = list(filter(lambda a,flags=flib_flags:a not in flags,sys.argv))
457   - fc_flags = list(filter(re.compile(r'[-][-]((f(77|90)(flags|exec)|opt|arch)=|(debug|noopt|noarch|help[-]fcompiler))').match,sys.argv[1:]))
458   - sys.argv = list(filter(lambda a,flags=fc_flags:a not in flags,sys.argv))
  454 + sys.argv = [_m for _m in sys.argv if _m not in f2py_flags2]
  455 + flib_flags = [_m for _m in sys.argv[1:] if
  456 + re.compile(r'[-][-]((f(90)?compiler([-]exec|)|compiler)=|help[-]compiler)').match(_m)]
  457 + sys.argv = [_m for _m in sys.argv if not _m in flib_flags]
  458 + fc_flags = [_m for _m in sys.argv[1:] if
  459 + re.compile(r'[-][-]((f(77|90)(flags|exec)|opt|arch)=|(debug|noopt|noarch|help[-]fcompiler))').match(_m)]
  460 + sys.argv = [_m for _m in sys.argv if not _m in fc_flags]
459 461
460 462 if 1:
461 463 del_list = []
@@ -481,8 +483,11 @@ def run_compile():
481 483 i = flib_flags.index(s)
482 484 del flib_flags[i]
483 485 assert len(flib_flags)<=2,`flib_flags`
484   - setup_flags = list(filter(re.compile(r'[-][-](verbose)').match,sys.argv[1:]))
485   - sys.argv = list(filter(lambda a,flags=setup_flags:a not in flags,sys.argv))
  486 +
  487 + setup_flags = [_m for _m in sys.argv[1:] if
  488 + re.compile(r'[-][-](verbose)').match(_m)]
  489 + sys.argv = [_m for _m in sys.argv if not _m in setup_flags]
  490 +
486 491 if '--quiet' in f2py_flags:
487 492 setup_flags.append('--quiet')
488 493
2  numpy/testing/utils.py
@@ -1050,7 +1050,7 @@ def decorate_methods(cls, decorator, testmatch=None):
1050 1050 # delayed import to reduce startup time
1051 1051 from inspect import isfunction
1052 1052
1053   - methods = list(filter(isfunction, cls_attr.values()))
  1053 + methods = [_m for _m in cls_attr.values() if isfunction(_m)]
1054 1054 for function in methods:
1055 1055 try:
1056 1056 if hasattr(function, 'compat_func_name'):

0 comments on commit 9a73697

Please sign in to comment.
Something went wrong with that request. Please try again.