本文整理汇总了Python中re.re函数的典型用法代码示例。如果您正苦于以下问题:Python re函数的具体用法?Python re怎么用?Python re使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了re函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_triple_double_quotes
def check_triple_double_quotes(self, definition, docstring):
r'''D300: Use """triple double quotes""".
For consistency, always use """triple double quotes""" around
docstrings. Use r"""raw triple double quotes""" if you use any
backslashes in your docstrings. For Unicode docstrings, use
u"""Unicode triple-quoted strings""".
Note: Exception to this is made if the docstring contains
""" quotes in its body.
'''
if docstring:
if '"""' in ast.literal_eval(docstring):
# Allow ''' quotes if docstring contains """, because
# otherwise """ quotes could not be expressed inside
# docstring. Not in PEP 257.
regex = re(r"[uU]?[rR]?'''[^'].*")
else:
regex = re(r'[uU]?[rR]?"""[^"].*')
if not regex.match(docstring):
illegal_matcher = re(r"""[uU]?[rR]?("+|'+).*""")
illegal_quotes = illegal_matcher.match(docstring).group(1)
return violations.D300(illegal_quotes)
示例2: run_pep257
def run_pep257():
log.setLevel(logging.DEBUG)
opt_parser = get_option_parser()
# setup the logger before parsing the config file, so that command line
# arguments for debug / verbose will be printed.
options, arguments = opt_parser.parse_args()
setup_stream_handlers(options)
# We parse the files before opening the config file, since it changes where
# we look for the file.
options = get_options(arguments, opt_parser)
if not validate_options(options):
return INVALID_OPTIONS_RETURN_CODE
# Setup the handler again with values from the config file.
setup_stream_handlers(options)
collected = collect(arguments or ['.'],
match=re(options.match + '$').match,
match_dir=re(options.match_dir + '$').match)
log.debug("starting pep257 in debug mode.")
Error.explain = options.explain
Error.source = options.source
collected = list(collected)
checked_codes = get_checked_error_codes(options)
errors = check(collected, select=checked_codes)
code = NO_VIOLATIONS_RETURN_CODE
count = 0
for error in errors:
sys.stderr.write('%s\n' % error)
code = VIOLATIONS_RETURN_CODE
count += 1
if options.count:
print(count)
return code
示例3: run_pep257
def run_pep257():
log.setLevel(logging.DEBUG)
opt_parser = get_option_parser()
# setup the logger before parsing the config file, so that command line
# arguments for debug / verbose will be printed.
options, arguments = opt_parser.parse_args()
setup_stream_handler(options)
# We parse the files before opening the config file, since it changes where
# we look for the file.
options = get_options(arguments, opt_parser)
# Setup the handler again with values from the config file.
setup_stream_handler(options)
collected = collect(arguments or ['.'],
match=re(options.match + '$').match,
match_dir=re(options.match_dir + '$').match)
log.debug("starting pep257 in debug mode.")
Error.explain = options.explain
Error.source = options.source
collected = list(collected)
errors = check(collected, ignore=options.ignore.split(','))
code = 0
count = 0
for error in errors:
sys.stderr.write('%s\n' % error)
code = 1
count += 1
if options.count:
print(count)
return code
示例4: main
def main(options, arguments):
Error.explain = options.explain
Error.source = options.source
collected = collect(arguments or ['.'],
match=re(options.match + '$').match,
match_dir=re(options.match_dir + '$').match)
code = 0
for error in check(collected, ignore=options.ignore.split(',')):
sys.stderr.write('%s\n' % error)
code = 1
return code
示例5: main
def main(options, arguments):
if options.debug:
log.setLevel(logging.DEBUG)
log.debug("starting pep257 in debug mode.")
Error.explain = options.explain
Error.source = options.source
collected = collect(arguments or ['.'],
match=re(options.match + '$').match,
match_dir=re(options.match_dir + '$').match)
code = 0
for error in check(collected, ignore=options.ignore.split(',')):
sys.stderr.write('%s\n' % error)
code = 1
return code
示例6: _get_ignore_decorators
def _get_ignore_decorators(config):
"""Return the `ignore_decorators` as None or regex."""
if config.ignore_decorators: # not None and not ''
ignore_decorators = re(config.ignore_decorators)
else:
ignore_decorators = None
return ignore_decorators
示例7: is_public
def is_public(self):
# Check if we are a setter/deleter method, and mark as private if so.
for decorator in self.decorators:
# Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo'
if re(r"^{0}\.".format(self.name)).match(decorator.name):
return False
name_is_public = not self.name.startswith('_') or is_magic(self.name)
return self.parent.is_public and name_is_public
示例8: _get_leading_words
def _get_leading_words(line):
"""Return any leading set of words from `line`.
For example, if `line` is " Hello world!!!", returns "Hello world".
"""
result = re("[A-Za-z ]+").match(line.strip())
if result is not None:
return result.group()
示例9: keys
def keys(self, pattern):
""" Add abilite to retrieve a list of keys with wildcard pattern.
:returns: List keys
"""
offset = len(self.make_key(''))
mask = re(fnmatch.translate(self.make_key(pattern)))
return [k[offset:] for k in self._cache.keys() if mask.match(k)]
示例10: is_public
def is_public(self):
"""Return True iff this method should be considered public."""
# Check if we are a setter/deleter method, and mark as private if so.
for decorator in self.decorators:
# Given 'foo', match 'foo.bar' but not 'foobar' or 'sfoo'
if re(r"^{}\.".format(self.name)).match(decorator.name):
return False
name_is_public = (not self.name.startswith('_') or
self.name in VARIADIC_MAGIC_METHODS or
self.is_magic)
return self.parent.is_public and name_is_public
示例11: parse_options
def parse_options(args=None, config=True, **overrides): # noqa
""" Parse options from command line and configuration files.
:return argparse.Namespace:
"""
if args is None:
args = []
# Parse args from command string
options = PARSER.parse_args(args)
options.file_params = dict()
options.linter_params = dict()
# Override options
for k, v in overrides.items():
passed_value = getattr(options, k, _Default())
if isinstance(passed_value, _Default):
setattr(options, k, _Default(v))
# Compile options from ini
if config:
cfg = get_config(str(options.options))
for k, v in cfg.default.items():
LOGGER.info('Find option %s (%s)', k, v)
passed_value = getattr(options, k, _Default())
if isinstance(passed_value, _Default):
setattr(options, k, _Default(v))
# Parse file related options
for name, opts in cfg.sections.items():
if not name.startswith('pylama'):
continue
if name == cfg.default_section:
continue
name = name[7:]
if name in LINTERS:
options.linter_params[name] = dict(opts)
continue
mask = re(fnmatch.translate(name))
options.file_params[mask] = dict(opts)
# Postprocess options
opts = dict(options.__dict__.items())
for name, value in opts.items():
if isinstance(value, _Default):
setattr(options, name, process_value(name, value.value))
return options
示例12: Rue
def Rue(f,A="",s=0):
from random import choice
if f in G:f=G[f]
else:
c=f
f=k("",open(f).read())
if f.startswith("import "):
a=f.find("\n",8)
f=open(f[7:a]).read()+f[a:]
a=f.find("\nimport ")+1
while a:
b=f.find("\n",a+8)
f=f[:a]+open(f[a+7:b]).read()+f[b:]
a=f.find("\nimport ",a)+1
f=f.split("\n::=\n")
G[c]=f
c=""
R=[]
for lf,C in zip(range(len(f)-1,-1,-1),f):
R+=((re(R[0],16).sub,R[1] if len(R) == 2 else R[1:] or "",len(R) == 1) for R in (R.split("::=") for R in c.split("\n") if R))
while 1:
while 1:
c=C=C.replace("@@",A)
for p0,p1,p2 in R:
C=p0(choice(p1) if p2 else p1,C,1)
if c is not C:break
else:break
if D:print(" "*s+C)
if lf:break
a=C.find("}")
if a == -1:break
while 1:
b=C.rfind("{",0,a)
c=C.rfind(":",0,b)
f=C[c+1:b]
b=C[b+1:a]
C=C[:c]+(Smod[f](b) if f in Smod else Rue(f,b,s+1))+C[a+1:]
a=C.find("}",c)
if a == -1:break
return C
示例13: highlighter
def highlighter(text, terms):
if not terms:
return text
def highlight(match):
return '<span class="highlight">%s</span>' % match.groups(0)[0]
return re(r'(%s)' % '|'.join(escape(term) for term in terms), IGNORECASE).sub(highlight, text)
示例14: re
from re import compile as re
from iterkit import all
part_finder = re(r'(_+|\d+|[A-Z]+(?![a-z])|[A-Z][a-z]+|[A-Z]+|[a-z]+)')
class CaseString(object):
def __init__(
self,
string = None,
parts = None,
prefix = None,
suffix = None
):
# UPPER_CASE
# lower_case
# TitleCase
# camelCase
# TitleCase1_2
self.string = string
if parts is not None:
self.parts = parts
self.prefix = ''
self.suffix = ''
else:
subparts = part_finder.findall(string)
示例15: _get_matches
def _get_matches(config):
"""Return the `match` and `match_dir` functions for `config`."""
match_func = re(config.match + '$').match
match_dir_func = re(config.match_dir + '$').match
return match_func, match_dir_func