def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
python类fnmatchcase()的实例源码
def wildcard_match(self, wildcard):
if hasattr(self, 'nkd_fnmatchcase'):
fnmatchcase = self.nkd_fnmatchcase
else:
from fnmatch import fnmatchcase
self.nkd_fnmatchcase = fnmatchcase
return [ x for x in self if fnmatchcase(x, wildcard) ]
#------------------------------------------------------------------------------
# [ multi_wildcard_match method ] (list)
# returns a list of items that match one or more | separated wildcards passed as string
#------------------------------------------------------------------------------
def wildcard_match(self, wildcard):
from fnmatch import fnmatchcase
return fnmatchcase(self, wildcard)
# convert string to normalized UTF-8 in Python 2 and 3 (##TODO: convert to XUnicode with attributes?)
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
def update_matching(dct, key_pattern, new_dct):
for key in dct:
if fnmatch.fnmatchcase(key, key_pattern):
new_dct[key] = dct[key]
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches at least one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
def matches_patterns(path, patterns=None):
"""
Return True or False depending on whether the ``path`` should be
ignored (if it matches any pattern in ``ignore_patterns``).
"""
if patterns is None:
patterns = []
for pattern in patterns:
if fnmatch.fnmatchcase(path, pattern):
return True
return False
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
def match(self, props):
"""
Check whether the given props match all the conditions.
"""
def match_condition(props, cond):
"""
Check whether the given props match the given condition.
"""
try:
prop, op, ref = props[cond[0]], cond[1], cond[2]
except KeyError:
return False
if op == "~":
return fnmatch.fnmatchcase(prop.lower(), ref.lower())
elif op == "=":
return prop == ref
elif op == ">":
return prop > ref
elif op == "<":
return prop < ref
else:
return False
#
return functools.reduce(operator.and_,
[ match_condition(props, cond) \
for cond in self.conditions ],
True)
def matches_patterns(path, patterns=None):
"""
Return True or False depending on whether the ``path`` should be
ignored (if it matches any pattern in ``ignore_patterns``).
"""
if patterns is None:
patterns = []
for pattern in patterns:
if fnmatch.fnmatchcase(path, pattern):
return True
return False
def find_package_modules(package, mask):
import fnmatch
if (hasattr(package, "__loader__") and
hasattr(package.__loader__, '_files')):
path = package.__name__.replace(".", os.path.sep)
mask = os.path.join(path, mask)
for fnm in package.__loader__._files.iterkeys():
if fnmatch.fnmatchcase(fnm, mask):
yield os.path.splitext(fnm)[0].replace(os.path.sep, ".")
else:
path = package.__path__[0]
for fnm in os.listdir(path):
if fnmatch.fnmatchcase(fnm, mask):
yield "%s.%s" % (package.__name__, os.path.splitext(fnm)[0])
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches at least one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches at least one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
def copy_everything(job, options):
"""
Download the file list and copy all the files.
"""
# Set up the IO stores.
in_store = IOStore.get(options.in_store)
out_store = IOStore.get(options.out_store)
batch_count = 0;
# List all the files.
blobs_iterator = in_store.list_input_directory("", recursive=True)
# Make an iterator that filters them
filtered_iterator = (x for x in blobs_iterator if
fnmatch.fnmatchcase(x, options.pattern))
# Batch them up
for batch in group(filtered_iterator, options.batch_size):
# For every batch, strip out any Nones that got put in when grouping
batch = [x for x in batch if x is not None]
# Copy everything in that batch
job.addChildJobFn(copy_batch, options, batch, cores=1, memory="1G",
disk="10G")
batch_count += 1
if batch_count % 10 == 0:
RealtimeLogger.info("Queued {} batches...".format(
batch_count))
RealtimeLogger.info("Queued {} total batches".format(batch_count))
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches at least one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)
def matches_patterns(path, patterns=None):
"""
Return True or False depending on whether the ``path`` should be
ignored (if it matches any pattern in ``ignore_patterns``).
"""
if patterns is None:
patterns = []
for pattern in patterns:
if fnmatch.fnmatchcase(path, pattern):
return True
return False
def fnmatchcase(filename, pattern):
cached_pattern = _get_cached_pattern(pattern)
return cached_pattern.match(filename) is not None
def fnmatch(filename, pattern):
filename = os.path.normcase(filename)
pattern = os.path.normcase(pattern)
return fnmatchcase(filename, pattern)
def _build_filter(*patterns):
"""
Given a list of patterns, return a callable that will be true only if
the input matches at least one of the patterns.
"""
return lambda name: any(fnmatchcase(name, pat=pat) for pat in patterns)