本文整理汇总了Python中sre_compile.compile函数的典型用法代码示例。如果您正苦于以下问题:Python compile函数的具体用法?Python compile怎么用?Python compile使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了compile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, runtimePath) :
self.availRuleNames = []
basePath = os.path.join(runtimePath, "rules")
ruleFiles = os.listdir(basePath)
rulePattern = sre_compile.compile("^(.*)\.py$")
for eachRuleFile in ruleFiles :
if os.path.isfile(os.path.join(basePath, eachRuleFile)) :
ruleMatch = rulePattern.match(eachRuleFile)
if ruleMatch != None and eachRuleFile.find("__init__") == -1 :
ruleName = ruleMatch.group(1)
self.availRuleNames.append(ruleName)
self.availRuleCount = len(self.availRuleNames)
self.availRuleModules = {}
self.loadedRule = []
self.rules = []
self.preprocessRules = []
self.functionNameRules = []
self.functionScopeRules = []
self.typeNameRules = []
self.typeScopeRules = []
self.lineRules = []
self.fileEndRules = []
self.fileStartRules = []
self.projectRules = []
self.rollBackImporter = None
示例2: _compile
def _compile(pattern, flags):
# internal: compile pattern
if isinstance(flags, RegexFlag):
flags = flags.value
try:
return _cache[type(pattern), pattern, flags]
except KeyError:
pass
if isinstance(pattern, Pattern):
if flags:
raise ValueError(
"cannot process flags argument with a compiled pattern")
return pattern
if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
p = sre_compile.compile(pattern, flags)
if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE:
# Drop the oldest item
try:
del _cache[next(iter(_cache))]
except (StopIteration, RuntimeError, KeyError):
pass
_cache[type(pattern), pattern, flags] = p
return p
示例3: compile_regexp_to_noncapturing
def compile_regexp_to_noncapturing(pattern, flags=0):
"""
Convert all grouping parentheses in the given regexp pattern to
non-capturing groups, and return the result. E.g.:
>>> from nltk.internals import compile_regexp_to_noncapturing
>>> compile_regexp_to_noncapturing('ab(c(x+)(z*))?d')
'ab(?:c(?:x+)(?:z*))?d'
:type pattern: str
:rtype: str
"""
def convert_regexp_to_noncapturing_parsed(parsed_pattern):
res_data = []
for key, value in parsed_pattern.data:
if key == sre_constants.SUBPATTERN:
index, subpattern = value
value = (None, convert_regexp_to_noncapturing(subpattern))
elif key == sre_constants.GROUPREF:
raise ValueError('Regular expressions with back-references are not supported: {0}'.format(pattern))
res_data.append((key, value))
parsed_pattern.data = res_data
parsed_pattern.pattern.groups = 1
parsed_pattern.pattern.groupdict = {}
return parsed_pattern
return sre_compile.compile(convert_regexp_to_noncapturing_parsed(sre_parse.parse(pattern)))
示例4: _compile
def _compile(*key):
pattern, flags = key
bypass_cache = flags & DEBUG
if not bypass_cache:
cachekey = (type(key[0]),) + key
p = _cache.get(cachekey)
if p is not None:
return p
if isinstance(pattern, _pattern_type):
if flags:
raise ValueError("Cannot process flags argument with a compiled pattern")
return pattern
else:
if not sre_compile.isstring(pattern):
raise TypeError, "first argument must be string or compiled pattern"
try:
p = sre_compile.compile(pattern, flags)
except error as v:
raise error, v
if not bypass_cache:
if len(_cache) >= _MAXCACHE:
_cache.clear()
_cache[cachekey] = p
return p
示例5: _compile
def _compile(*key):
# internal: compile pattern
taint = _get_taint(key[0])
if taint is not None: # can't hash the set
taint = tuple(taint)
cachekey = (type(key[0]), key, taint)
p = re._cache.get(cachekey)
if p is not None:
return p
pattern, flags = key
if isinstance(pattern, re._pattern_type):
if flags:
raise ValueError("Cannot process flags argument with"
" a compiled pattern")
return pattern
if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled"
" pattern")
p = sre_compile.compile(pattern, flags)
if len(re._cache) >= re._MAXCACHE:
re._cache.clear()
re._cache[cachekey] = p
return p
示例6: __init__
def __init__(self, lexicons, init_state=None, flags=0):
# All the regexp magic below is copied from re.Scanner from
# the standard library.
import sre_compile
import sre_parse
from sre_constants import BRANCH, SUBPATTERN
if init_state is None:
init_state = State()
if not hasattr(init_state, 'start'):
init_state.start = None
self.init_state = init_state
self.lexicons = lexicons
self.scanners = {}
for start, lexicon in lexicons.iteritems():
# combine phrases into a compound pattern
p, a = [], []
s = sre_parse.Pattern()
s.flags = flags
for phrase, action in lexicon:
p.append(sre_parse.SubPattern(s, [
(SUBPATTERN, (len(p)+1,
sre_parse.parse(phrase, flags))),
]))
a.append(action)
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
s.groups = len(p)
self.scanners[start] = sre_compile.compile(p).match, a
示例7: _compile
def _compile(pattern, flags):
# internal: compile pattern
try:
p, loc = _cache[type(pattern), pattern, flags]
if loc is None or loc == _locale.setlocale(_locale.LC_CTYPE):
return p
except KeyError:
pass
if isinstance(pattern, _pattern_type):
if flags:
raise ValueError(
"cannot process flags argument with a compiled pattern")
return pattern
if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
p = sre_compile.compile(pattern, flags)
if not (flags & DEBUG):
if len(_cache) >= _MAXCACHE:
_cache.clear()
if p.flags & LOCALE:
if not _locale:
return p
loc = _locale.setlocale(_locale.LC_CTYPE)
else:
loc = None
_cache[type(pattern), pattern, flags] = p, loc
return p
示例8: __init__
def __init__(self, lexicon, flags=FLAGS):
self.actions = [None]
# combine phrases into a compound pattern
s = sre_parse.Pattern()
s.flags = flags
p = []
# NOTE(kgibbs): These lines must be added to make this file work under
# Python 2.2, which is commonly used at Google.
def enumerate(obj):
i = -1
for item in obj:
i += 1
yield i, item
# NOTE(kgibbs): End changes.
for idx, token in enumerate(lexicon):
phrase = token.pattern
try:
subpattern = sre_parse.SubPattern(s,
[(SUBPATTERN, (idx + 1, sre_parse.parse(phrase, flags)))])
except sre_constants.error:
raise
p.append(subpattern)
self.actions.append(token)
s.groups = len(p)+1 # NOTE(guido): Added to make SRE validation work
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = sre_compile.compile(p)
示例9: Sub
def Sub(self, pattern, repl, s):
"""Replace the string for the pattern by the paramter repl, caching the compiled regexp."""
# for example: s='a1234a' ,repl='OOOO' pattern = r'd+'
# result is 'aOOOOa'
#
if not pattern in self._regexp_compile_cache:
self._regexp_compile_cache[pattern] = sre_compile.compile(pattern)
return self._regexp_compile_cache[pattern].sub(repl,s)
示例10: Match
def Match(self, pattern, s):
"""Matches the string with the pattern, caching the compiled regexp."""
# The regexp compilation caching is inlined in both Match and Search for
# performance reasons; factoring it out into a separate function turns out
# to be noticeably expensive.
if not pattern in self._regexp_compile_cache:
self._regexp_compile_cache[pattern] = sre_compile.compile(pattern)
return self._regexp_compile_cache[pattern].match(s)
示例11: _compile_typed
def _compile_typed(text_bytes_type, pattern, flags):
# internal: compile pattern
if isinstance(pattern, _pattern_type):
if flags:
raise ValueError(
"Cannot process flags argument with a compiled pattern")
return pattern
if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
return sre_compile.compile(pattern, flags)
示例12: __init__
def __init__(self, lexicon, flags=0):
from sre_constants import BRANCH, SUBPATTERN
self.lexicon = lexicon
p = []
s = sre_parse.Pattern()
s.flags = flags
for (phrase, action) in lexicon:
p.append(sre_parse.SubPattern(s, [(SUBPATTERN, (len(p) + 1, sre_parse.parse(phrase, flags)))]))
s.groups = len(p) + 1
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
self.scanner = sre_compile.compile(p)
示例13: __init__
def __init__(self, lexicon):
from sre_constants import BRANCH, SUBPATTERN
self.lexicon = lexicon
# combine phrases into a compound pattern
p = []
s = sre_parse.Pattern()
for phrase, action in lexicon:
p.append(sre_parse.SubPattern(s, [(SUBPATTERN, (len(p), sre_parse.parse(phrase)))]))
p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
s.groups = len(p)
self.scanner = sre_compile.compile(p)
示例14: _compile
def _compile(*key):
# internal: compile pattern
p = _cache.get(key)
if p is not None:
return p
pattern, flags = key
if type(pattern) not in sre_compile.STRING_TYPES:
return pattern
try:
p = sre_compile.compile(pattern, flags)
except error, v:
raise error, v # invalid expression
示例15: _get_group_pattern
def _get_group_pattern(self,flags):
# combine phrases into a compound pattern
patterns = []
sub_pattern = sre_parse.Pattern()
sub_pattern.flags = flags
for phrase, action in self.lexicon:
patterns.append(sre_parse.SubPattern(sub_pattern, [
(SUBPATTERN, (len(patterns) + 1, sre_parse.parse(phrase, flags))),
]))
sub_pattern.groups = len(patterns) + 1
group_pattern = sre_parse.SubPattern(sub_pattern, [(BRANCH, (None, patterns))])
return sre_compile.compile(group_pattern)