我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用fnmatch.filter()。
def assign_brandenburg(self, *args, **options): brandenburg_state = State.objects.get(name='Brandenburg') excel_file = pd.ExcelFile(options['filename']) df = excel_file.parse('Brandenburg') assigned_auths = defaultdict(list) locations = {} for _, row in df.iterrows(): auth = SupervisionAuthority.objects.get(state=brandenburg_state, name=row['name']) locations[auth] = GEOSGeometry('POINT(%f %f)' % (row['lng'], row['lat']), srid=4326) assigned_districts = row[u'Landkreis-Zuständigkeit'].splitlines() for district_name in assigned_districts: districts = District.objects.filter(part_of=brandenburg_state, name=district_name) if len(districts) != 1: print(district_name) print(districts) else: assigned_auths[districts[0]].append(auth) for nursinghome in NursingHome.objects.filter(supervision_authority__isnull=True, state=brandenburg_state): district = District.objects.get(geom__covers=nursinghome.geo) auths = assigned_auths[district] if len(auths) == 1: nursinghome.supervision_authority = auths[0] nursinghome.save() else: min_distance = None best_auth = None for auth, point in locations.items(): if auth not in auths: continue dist = NursingHome.objects.filter(pk=nursinghome.pk ).annotate(distance=Distance('geo', point)) dist = dist[0].distance.m if min_distance is None or dist < min_distance: min_distance = dist best_auth = auth nursinghome.supervision_authority = best_auth nursinghome.save()
def assign_rheinlandpfalz(self, *args, **options): rp_state = State.objects.get(name='Rheinland-Pfalz') excel_file = pd.ExcelFile(options['filename']) df = excel_file.parse('Rheinland-Pfalz') assigned = defaultdict(list) for _, row in df.iterrows(): auth = SupervisionAuthority.objects.get(state=rp_state, name=row['name']) district_names = row[u'Landkreis-Zuständigkeit'].splitlines() for district_name in district_names: only = None if '|' in district_name: district_name, only = district_name.split('|') only = only.split(',') districts = District.objects.filter(part_of=rp_state, name=district_name) if len(districts) == 0: districts = District.objects.filter(part_of=rp_state, name__contains=district_name) if len(districts) == 0: districts = District.objects.filter(part_of=rp_state, name__contains=district_name.split()[0]) if len(districts) == 0: districts = District.objects.filter(part_of=rp_state, name__istartswith=re.sub('\W', '', district_name)) if len(districts) > 1: if 'Kreis' in district_name: districts = districts.filter(kind_detail__contains='Landkreis') if 'Stadt' in district_name: districts = districts.filter(kind_detail__contains='Stadt') if len(districts) != 1: print(districts) print(u'District not one: %s' % district_name) continue assigned[auth].append((districts[0], only)) for auth, district_list in assigned.items(): for district, only in district_list: if only is None: NursingHome.objects.filter(state=rp_state, district=district, supervision_authority__isnull=True).update(supervision_authority=auth) continue for muni_name in only: muni_name = muni_name.strip() munis = Municipality.objects.filter(part_of=district, name__contains=muni_name) if len(munis) > 1: munis = Municipality.objects.filter(part_of=district, name=muni_name) if len(munis) != 1: print('Did not find %s' % muni_name) continue muni = munis[0] NursingHome.objects.filter(state=rp_state, district=district, supervision_authority__isnull=True, geo__coveredby=muni.geom).update(supervision_authority=auth)
def find_data_files(self, package, src_dir): """Return filenames for package's data files in 'src_dir'""" patterns = self._get_platform_patterns( self.package_data, package, src_dir, ) globs_expanded = map(glob, patterns) # flatten the expanded globs into an iterable of matches globs_matches = itertools.chain.from_iterable(globs_expanded) glob_files = filter(os.path.isfile, globs_matches) files = itertools.chain( self.manifest_files.get(package, []), glob_files, ) return self.exclude_data_files(package, src_dir, files)
def exclude_data_files(self, package, src_dir, files): """Filter filenames for package's data files in 'src_dir'""" files = list(files) patterns = self._get_platform_patterns( self.exclude_package_data, package, src_dir, ) match_groups = ( fnmatch.filter(files, pattern) for pattern in patterns ) # flatten the groups of matches into an iterable of matches matches = itertools.chain.from_iterable(match_groups) bad = set(matches) keepers = ( fn for fn in files if fn not in bad ) # ditch dupes return list(_unique_everseen(keepers))
def exclude_data_files(self, package, src_dir, files): """Filter filenames for package's data files in 'src_dir'""" globs = (self.exclude_package_data.get('', []) + self.exclude_package_data.get(package, [])) bad = [] for pattern in globs: bad.extend( fnmatch.filter( files, os.path.join(src_dir, convert_path(pattern)) ) ) bad = dict.fromkeys(bad) seen = {} return [ f for f in files if f not in bad and f not in seen and seen.setdefault(f,1) # ditch dupes ]
def exclude_data_files(self, package, src_dir, files): """Filter filenames for package's data files in 'src_dir'""" globs = (self.exclude_package_data.get('', []) + self.exclude_package_data.get(package, [])) bad = [] for pattern in globs: bad.extend( fnmatch.filter( files, os.path.join(src_dir, convert_path(pattern)) ) ) bad = dict.fromkeys(bad) seen = {} return [ f for f in files if f not in bad and f not in seen and seen.setdefault(f, 1) # ditch dupes ]
def test_diff_roles(): from ldap2pg.manager import SyncManager, Role, RoleSet m = SyncManager() pgroles = RoleSet([ Role('drop-me'), Role('alter-me'), Role('nothing'), ]) ldaproles = RoleSet([ Role('alter-me', options=dict(LOGIN=True)), Role('nothing'), Role('create-me') ]) queries = [q.args[0] for q in m.diff(pgroles, set(), ldaproles, set())] assert fnfilter(queries, 'ALTER ROLE "alter-me" WITH* LOGIN*;') assert fnfilter(queries, 'CREATE ROLE "create-me" *;') assert fnfilter(queries, '*DROP ROLE "drop-me";*') assert not fnfilter(queries, '*nothing*')
def exclude_data_files(self, package, src_dir, files): """Filter filenames for package's data files in 'src_dir'""" globs = ( self.exclude_package_data.get('', []) + self.exclude_package_data.get(package, []) ) bad = set( item for pattern in globs for item in fnmatch.filter( files, os.path.join(src_dir, convert_path(pattern)), ) ) seen = collections.defaultdict(itertools.count) return [ fn for fn in files if fn not in bad # ditch dupes and not next(seen[fn]) ]
def _build(inputs): paths = [] for f in inputs: if os.path.isdir(f): # Walk through the directory and get all PDF files # Credit: https://stackoverflow.com/a/2186565/4856091 for root, dirnames, filenames in os.walk(f): for filename in fnmatch.filter(filenames, "*.pdf"): paths.append(os.path.join(root, filename)) elif f.endswith(".pdf"): paths.append(f) else: # Get the contents as list of files _files = [line.strip() for line in open(f).readlines()] _files = [_f for _f in _files if (not _f.startswith("#")) and (_f != "")] paths += _files return paths
def __search_folders(self, key): result = SearchResult('FOLDERS') folders = [] for rootfolder in self.__settings['folders']: dirnames = next(os.walk(rootfolder))[1] for dirname in fnmatch.filter(dirnames, '*{}*'.format(key)): folders.append(os.path.join(rootfolder, dirname)) if len(folders) == 0: result.add_message('No folder to remove.', None) else: for folder in folders: result.add_message( 'Folder \'{}\' will be removed.' .format(folder) ) result.add_action( lambda f=folder: self.__remove_folder(f) ) return result
def _process_paths(self): for path in self.path_list: if os.path.isfile(path): self._process(path) elif os.path.isdir(path): for root, dirs, files in os.walk(path): for filename in fnmatch.filter(files, self.extension): self._process(os.path.join(root, filename)) else: raise TypeError('%s should be either dir or file' % path) if self.pretend: logging.info('No actual changes was done, run without \'-p\' to overwrite source files') logging.info('%d files processed' % self.files_processed)
def get_mosfit_hash(salt=u''): """Return a unique hash for the MOSFiT code.""" import fnmatch import os dir_path = os.path.dirname(os.path.realpath(__file__)) matches = [] for root, dirnames, filenames in os.walk(dir_path): for filename in fnmatch.filter(filenames, '*.py'): matches.append(os.path.join(root, filename)) matches = list(sorted(list(matches))) code_str = salt for match in matches: with codecs.open(match, 'r', 'utf-8') as f: code_str += f.read() return hashlib.sha512(hash_bytes(code_str)).hexdigest()[:16]
def test(self): """ Runs the binutils `make check` """ ret = build.make(self.src_dir, extra_args='check', ignore_status=True) errors = 0 for root, _, filenames in os.walk(self.src_dir): for filename in fnmatch.filter(filenames, '*.log'): filename = os.path.join(root, filename) logfile = filename[:-4] + ".log" os.system('cp ' + logfile + ' ' + self.logdir) with open(logfile) as result: for line in result.readlines(): if line.startswith('FAIL'): errors += 1 self.log.error(line) if errors: self.fail("%s test(s) failed, check the log for details." % errors) elif ret: self.fail("'make check' finished with %s, but no FAIL lines were " "found." % ret)
def test(self): """ Run the `make check` on ltrace """ ret = build.make(self.src_lt, extra_args='check', ignore_status=True) errors = 0 for root, _, filenames in os.walk('.'): for filename in fnmatch.filter(filenames, '*.log'): filename = os.path.join(root, filename) shutil.copy(filename, self.logdir) with open(filename) as result: for line in result.readlines(): if line.startswith('FAIL'): errors += 1 self.log.error(line) if errors: self.fail("%s test(s) failed, check the log for details." % errors) elif ret: self.fail("'make check' finished with %s, but no FAIL lines were " "found." % ret)
def _GetFilesWithExt(root_dir, ext): """Gets all files with a given extension. Args: root_dir: Directory in which to search for files. ext: Extension to look for (including dot) Returns: A list of absolute paths to files that match. """ files = [] for root, _, filenames in os.walk(root_dir): basenames = fnmatch.filter(filenames, '*.' + ext) files.extend([os.path.join(root, basename) for basename in basenames]) return files
def _index(cls, path, types): if sys.version_info >= (3, 5): # Python version >=3.5 supports glob import glob for img_type in types: for filename in glob.iglob( (path + '/**/' + img_type), recursive=True ): f_base = os.path.basename(filename) cls._names.update({f_base: filename}) else: # Python version <=3.4 import fnmatch for root, dirnames, filenames in os.walk(path): for img_type in types: for f_base in fnmatch.filter(filenames, img_type): filename = os.path.join(root, f_base) cls._names.update({f_base: filename})
def filter(filenames, pattern): filtered_filenames = [] pattern = os.path.normcase(pattern) cached_pattern = _get_cached_pattern(pattern) if os.path is posixpath: # normcase on posix is NOP. Optimize it away from the loop. for filename in filenames: if cached_pattern.match(filename): filtered_filenames.append(filename) else: for filename in filenames: norm_name = os.path.normcase(filename) if cached_pattern.match(norm_name): filtered_filenames.append(filename) return filtered_filenames
def get_songs(): matches = [] num = 0 for root, dirnames, filenames in walk(folder_to_save_data): for filename in fnmatch.filter(filenames, '*.mp3'): filename = join(root, filename).replace( folder_to_save_data, '') filename = filename.replace('.mp3', '') if filename[0] == "/": filename = filename[1:] songname = filename if songname[-12:-11] == "-": songname = songname[:-12] songname = re.sub(r"[\(\[].*?[\)\]]", "", songname).strip() num += 1 matches.append({'file': filename, 'name': songname, 'id': num}) return matches
def get_files(directory, pattern, recursive=True): """ Return the full path to all files in directory matching the specified pattern. Arguments: directory (str): Directory path in which to look pattern (str): A glob pattern for filenames recursive (bool): Searches recursively if True Returns: A list of matching file paths """ # This yields an iterator which really speeds up looking through large, flat directories if recursive is False: it = glob.iglob(os.path.join(directory, pattern)) return it # If we want to recurse, use os.walk instead matches = list() for root, dirnames, filenames in os.walk(directory): matches.extend([os.path.join(root, ss) for ss in fnmatch.filter(filenames, pattern)]) return matches
def get_checkpoints_from_s3_path(path): import boto3 s3_resource = boto3.resource('s3') bucket_name, key_name = split_s3_bucket_key(path) bucket = s3_resource.Bucket(bucket_name) all_objects = list(bucket.objects.filter(Prefix=key_name)) all_keys = [o.key for o in all_objects] keys = fnmatch.filter(all_keys, S3_KEY_PATTERN) checkpoints = [] for f in keys: try: file_path = os.path.join(bucket_name, f) checkpoints.append(parse_checkpoint_s3_path(file_path)) except ValueError: continue return sorted(checkpoints, key=lambda cp: cp.start)
def run_per_file(config, ignore_paths=None, path=None, config_dir=None): ignore_paths = ignore_paths or [] path = path or os.getcwd() cmd = run_config(config, config_dir) print(cmd) run_cmds = [] patterns = PATTERNS.get(config.get('language')) paths = all_filenames_in_dir(path=path, ignore_paths=ignore_paths) for pattern in patterns: for filepath in fnmatch.filter(paths, pattern): run_cmds.append(cmd + [filepath]) pool = Pool() def result(run_cmd): _, out = run_command(run_cmd) return run_cmd[-1], out output = pool.map(result, run_cmds) return output
def ignore_patterns(*patterns): """Function that can be used as copytree() ignore parameter. Patterns is a sequence of glob-style patterns that are used to exclude files""" def _ignore_patterns(path, names): ignored_names = [] for pattern in patterns: ignored_names.extend(fnmatch.filter(names, pattern)) return set(ignored_names) return _ignore_patterns
def glob1(dirname, pattern): if not dirname: if isinstance(pattern, binary_type): dirname = os.curdir.encode('ASCII') else: dirname = os.curdir try: names = os.listdir(dirname) except OSError: return [] return fnmatch.filter(names, pattern)
def reimport(*paths): for p in paths: if os.path.isfile(p): print('Reimporting file: ' + p) _reimport(p) elif os.path.isdir(p): print('Reimporting dir: ' + p) for dir, _, files in os.walk(p): for f in fnmatch.filter(files, '*.go'): _reimport(dir + '/' + f) else: for f in glob.glob(p): print('Reimporinting file: ' + f) _reimport(f)