本文整理匯總了Python中fnmatch.translate方法的典型用法代碼示例。如果您正苦於以下問題:Python fnmatch.translate方法的具體用法?Python fnmatch.translate怎麽用?Python fnmatch.translate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類fnmatch
的用法示例。
在下文中一共展示了fnmatch.translate方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _extract_filter_matchers
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def _extract_filter_matchers(test_filters):
if not test_filters:
return (
(lambda _recipe_name: True),
(lambda _full_test_case_name: True),
)
return (
re.compile('|'.join([
fnmatch.translate(test_name.split(pattern)[0])
for pattern in test_filters
])).match,
re.compile('|'.join([
fnmatch.translate(pattern)
for pattern in test_filters
])).match
)
示例2: test_ufunc_override_not_implemented
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def test_ufunc_override_not_implemented(self):
class A(object):
def __array_ufunc__(self, *args, **kwargs):
return NotImplemented
msg = ("operand type(s) all returned NotImplemented from "
"__array_ufunc__(<ufunc 'negative'>, '__call__', <*>): 'A'")
with assert_raises_regex(TypeError, fnmatch.translate(msg)):
np.negative(A())
msg = ("operand type(s) all returned NotImplemented from "
"__array_ufunc__(<ufunc 'add'>, '__call__', <*>, <object *>, "
"out=(1,)): 'A', 'object', 'int'")
with assert_raises_regex(TypeError, fnmatch.translate(msg)):
np.add(A(), object(), out=1)
示例3: _glob_to_re
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def _glob_to_re(self, pattern):
"""Translate a shell-like glob pattern to a regular expression.
Return a string containing the regex. Differs from
'fnmatch.translate()' in that '*' does not match "special characters"
(which are platform-specific).
"""
pattern_re = fnmatch.translate(pattern)
# '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
# IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
# and by extension they shouldn't match such "special characters" under
# any OS. So change all non-escaped dots in the RE to match any
# character except the special characters (currently: just os.sep).
sep = os.sep
if os.sep == '\\':
# we're using a regex to manipulate a regex, so we need
# to escape the backslash twice
sep = r'\\\\'
escaped = r'\1[^%s]' % sep
pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re)
return pattern_re
示例4: __init__
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def __init__(
self, index_url="https://pypi.python.org/simple", hosts=('*',),
ca_bundle=None, verify_ssl=True, *args, **kw
):
Environment.__init__(self, *args, **kw)
self.index_url = index_url + "/" [:not index_url.endswith('/')]
self.scanned_urls = {}
self.fetched_urls = {}
self.package_pages = {}
self.allows = re.compile('|'.join(map(translate, hosts))).match
self.to_scan = []
use_ssl = (
verify_ssl
and ssl_support.is_available
and (ca_bundle or ssl_support.find_ca_bundle())
)
if use_ssl:
self.opener = ssl_support.opener_for(ca_bundle)
else:
self.opener = urllib.request.urlopen
示例5: glob_to_re
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def glob_to_re(pattern):
"""Translate a shell-like glob pattern to a regular expression.
Return a string containing the regex. Differs from
'fnmatch.translate()' in that '*' does not match "special characters"
(which are platform-specific).
"""
pattern_re = fnmatch.translate(pattern)
# '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
# IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
# and by extension they shouldn't match such "special characters" under
# any OS. So change all non-escaped dots in the RE to match any
# character except the special characters.
# XXX currently the "special characters" are just slash -- i.e. this is
# Unix-only.
pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', r'\1[^/]', pattern_re)
return pattern_re
示例6: _check_status
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def _check_status(self, status, res):
if status == '*':
return
res_status = res.status
if (isinstance(status, string_types) and '*' in status):
if re.match(fnmatch.translate(status), res_status, re.I):
return
if isinstance(status, (list, tuple)):
if res.status_int not in status:
raise AppError(
"Bad response: %s (not one of %s for %s)\n%s",
res_status, ', '.join(map(str, status)),
res.request.url, res)
return
if status is None:
if res.status_int >= 200 and res.status_int < 400:
return
raise AppError(
"Bad response: %s (not 200 OK or 3xx redirect for %s)\n%s",
res_status, res.request.url,
res)
if status != res.status_int:
raise AppError(
"Bad response: %s (not %s)", res_status, status)
示例7: __init__
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def __init__(
self, index_url="https://pypi.org/simple/", hosts=('*',),
ca_bundle=None, verify_ssl=True, *args, **kw
):
Environment.__init__(self, *args, **kw)
self.index_url = index_url + "/" [:not index_url.endswith('/')]
self.scanned_urls = {}
self.fetched_urls = {}
self.package_pages = {}
self.allows = re.compile('|'.join(map(translate, hosts))).match
self.to_scan = []
use_ssl = (
verify_ssl
and ssl_support.is_available
and (ca_bundle or ssl_support.find_ca_bundle())
)
if use_ssl:
self.opener = ssl_support.opener_for(ca_bundle)
else:
self.opener = urllib.request.urlopen
示例8: glob_to_re
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def glob_to_re(pattern):
"""Translate a shell-like glob pattern to a regular expression.
Return a string containing the regex. Differs from
'fnmatch.translate()' in that '*' does not match "special characters"
(which are platform-specific).
"""
pattern_re = fnmatch.translate(pattern)
# '?' and '*' in the glob pattern become '.' and '.*' in the RE, which
# IMHO is wrong -- '?' and '*' aren't supposed to match slash in Unix,
# and by extension they shouldn't match such "special characters" under
# any OS. So change all non-escaped dots in the RE to match any
# character except the special characters (currently: just os.sep).
sep = os.sep
if os.sep == '\\':
# we're using a regex to manipulate a regex, so we need
# to escape the backslash twice
sep = r'\\\\'
escaped = r'\1[^%s]' % sep
pattern_re = re.sub(r'((?<!\\)(\\\\)*)\.', escaped, pattern_re)
return pattern_re
示例9: __init__
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def __init__(
self, index_url="https://pypi.python.org/simple", hosts=('*',),
ca_bundle=None, verify_ssl=True, *args, **kw
):
Environment.__init__(self, *args, **kw)
self.index_url = index_url + "/"[:not index_url.endswith('/')]
self.scanned_urls = {}
self.fetched_urls = {}
self.package_pages = {}
self.allows = re.compile('|'.join(map(translate, hosts))).match
self.to_scan = []
use_ssl = (
verify_ssl
and ssl_support.is_available
and (ca_bundle or ssl_support.find_ca_bundle())
)
if use_ssl:
self.opener = ssl_support.opener_for(ca_bundle)
else:
self.opener = urllib.request.urlopen
示例10: filter
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def filter(names, pat):
"""Return the subset of the list NAMES that match PAT"""
import os,posixpath
result=[]
pat = os.path.normcase(pat)
if pat not in fnmatch._cache:
import re
res = fnmatch.translate(pat)
fnmatch._cache[pat] = re.compile(res)
match = fnmatch._cache[pat].match
if os.path is posixpath:
# normcase on posix is NOP. Optimize it away from the loop.
for name in names:
if match(name):
result.append(name)
else:
for name in names:
if match(os.path.normcase(name)):
result.append(name)
return result
示例11: filter_by
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def filter_by(prop_name, kwargs, objs):
"""
Utility method for filtering a list of objects by a property.
If the given property has a non empty value in kwargs, then
the list of objs is filtered by that value. Otherwise, the
list of objs is returned as is.
"""
prop_val = kwargs.pop(prop_name, None)
if prop_val:
if isinstance(prop_val, six.string_types):
regex = fnmatch.translate(prop_val)
results = [o for o in objs
if getattr(o, prop_name)
and re.search(regex, getattr(o, prop_name))]
else:
results = [o for o in objs
if getattr(o, prop_name) == prop_val]
return results
else:
return objs
示例12: multiglob_compile
# 需要導入模塊: import fnmatch [as 別名]
# 或者: from fnmatch import translate [as 別名]
def multiglob_compile(globs, prefix=False):
"""Generate a single "A or B or C" regex from a list of shell globs.
:param globs: Patterns to be processed by :mod:`fnmatch`.
:type globs: iterable of :class:`~__builtins__.str`
:param prefix: If ``True``, then :meth:`~re.RegexObject.match` will
perform prefix matching rather than exact string matching.
:type prefix: :class:`~__builtins__.bool`
:rtype: :class:`re.RegexObject`
"""
if not globs:
# An empty globs list should only match empty strings
return re.compile('^$')
elif prefix:
globs = [x + '*' for x in globs]
return re.compile('|'.join(fnmatch.translate(x) for x in globs))