当前位置: 首页>>代码示例>>Python>>正文


Python sre_parse.parse_template函数代码示例

本文整理汇总了Python中sre_parse.parse_template函数的典型用法代码示例。如果您正苦于以下问题:Python parse_template函数的具体用法?Python parse_template怎么用?Python parse_template使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了parse_template函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: parse_template

    def parse_template(self, pattern):
        """Parse template."""

        i = ReplaceTokens(self._original, use_format=self.use_format)
        iter(i)
        self.result = [self._empty]

        for t in i:
            if len(t) > 1:
                if self.use_format and t[0:1] == self._lc_bracket:
                    self.handle_format_group(t[1:-1].strip())
                else:
                    c = t[1:]
                    first = c[0:1]
                    if first.isdigit() and (self.use_format or len(c) == 3):
                        value = int(c, 8)
                        if value > 0xFF:
                            if self.binary:
                                # Re fails on octal greater than 0o377 or 0xFF
                                raise ValueError("octal escape value outside of range 0-0o377!")
                            self.result.append(compat.uchr(value))
                        else:
                            self.result.append(self.string_convert('\\%03o' % value))
                    elif not self.use_format and (c[0:1].isdigit() or c[0:1] == self._group):
                        self.handle_group(t)
                    elif c == self._lc:
                        self.single_case(i, _LOWER)
                    elif c == self._lc_span:
                        self.span_case(i, _LOWER)
                    elif c == self._uc:
                        self.single_case(i, _UPPER)
                    elif c == self._uc_span:
                        self.span_case(i, _UPPER)
                    elif c == self._end:
                        # This is here just as a reminder that \E is ignored
                        pass
                    elif (
                        not self.binary and
                        (first == self._unicode_narrow or (not NARROW and first == self._unicode_wide))
                    ):
                        value = int(t[2:], 16)
                        if value <= 0xFF:
                            self.result.append('\\%03o' % value)
                        else:
                            self.result.append(compat.uchr(value))
                    elif first == self._hex:
                        self.result.append('\\%03o' % int(t[2:], 16))
                    else:
                        self.result.append(t)
            else:
                self.result.append(t)

        if len(self.result) > 1:
            self.literal_slots.append(self._empty.join(self.result))
            del self.result[:]
            self.result.append(self._empty)
            self.slot += 1

        self._template = self._empty.join(self.literal_slots)
        self.groups, self.literals = sre_parse.parse_template(self._template, pattern)
开发者ID:PwnArt1st,项目名称:dotfiles,代码行数:60,代码来源:bre.py

示例2: __init__

 def __init__(self, pattern, template):
     """Initialize."""
     self.__original = template
     self.__back_ref = set()
     self.__add_back_references(CAP_TOKEN)
     self.__template = self.__escape_template(template)
     self.groups, self.literals = sre_parse.parse_template(self.__template, pattern)
开发者ID:dlwilson88,项目名称:RegReplace,代码行数:7,代码来源:rr_extended.py

示例3: _compile_repl

def _compile_repl(*key):
    p = _cache_repl.get(key)
    if (p is not None):
        return p
    (repl, pattern,) = key
    try:
        p = sre_parse.parse_template(repl, pattern)
    except error, v:
        raise error, v
开发者ID:Etnorazz,项目名称:LeapConductor,代码行数:9,代码来源:sre.py

示例4: _compile_repl

def _compile_repl(*key):
    # internal: compile replacement pattern
    p = _cache_repl.get(key)
    if p is not None:
        return p
    repl, pattern = key
    try:
        p = sre_parse.parse_template(repl, pattern)
    except error, v:
        raise error, v # invalid expression
开发者ID:1018365842,项目名称:FreeIMU,代码行数:10,代码来源:re.py

示例5: _compile_repl

def _compile_repl(repl, pattern):
    try:
        return _cache_repl[(repl, pattern)]
    except KeyError:
        pass
    p = sre_parse.parse_template(repl, pattern)
    if len(_cache_repl) >= _MAXCACHE:
        _cache_repl.clear()
    _cache_repl[(repl, pattern)] = p
    return p
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:10,代码来源:re.py

示例6: _compile_repl

def _compile_repl(repl, pattern):
    # internal: compile replacement pattern
    try:
        return _cache_repl[repl, pattern]
    except KeyError:
        pass
    p = sre_parse.parse_template(repl, pattern)
    if len(_cache_repl) >= _MAXCACHE:
        _cache_repl.clear()
    _cache_repl[repl, pattern] = p
    return p
开发者ID:alfonsodiecko,项目名称:PYTHON_DIST,代码行数:11,代码来源:re.py

示例7: _compile_repl

def _compile_repl(*key):
    # internal: compile replacement pattern
    p = _cache_repl.get(key)
    if p is not None:
        return p
    repl, pattern = key
    p = sre_parse.parse_template(repl, pattern)
    if len(_cache_repl) >= _MAXCACHE:
        _cache_repl.clear()
    _cache_repl[key] = p
    return p
开发者ID:LinkedModernismProject,项目名称:web_code,代码行数:11,代码来源:re.py

示例8: _compile_repl

def _compile_repl(*key):
    p = _cache_repl.get(key)
    if p is not None:
        return p
    repl, pattern = key
    try:
        p = sre_parse.parse_template(repl, pattern)
    except error as v:
        raise error, v

    if len(_cache_repl) >= _MAXCACHE:
        _cache_repl.clear()
    _cache_repl[key] = p
    return p
开发者ID:OwenPratt,项目名称:AbletonLive9_RemoteScripts,代码行数:14,代码来源:re.py

示例9: _compile_repl

def _compile_repl(repl, pattern):
    # internal: compile replacement pattern
    try:
        #fix me brython
        #return _cache_repl[repl, pattern]
        return _cache_repl["%s:%s" % (repl, pattern)]
    except KeyError:
        pass
    p = sre_parse.parse_template(repl, pattern)
    if len(_cache_repl) >= _MAXCACHE:
        _cache_repl.clear()
    _cache_repl["%s:%s" % (repl, pattern)] = p
    #fix me brython
    #_cache_repl[repl, pattern] = p
    return p
开发者ID:D3f0,项目名称:pinguinoweb,代码行数:15,代码来源:pyre.py

示例10: _expand

 def _expand(self, m, template):
     # XXX This code depends on internals of the regular expression
     # engine!  There's no standard API to do a substitution when you
     # have already found the match.  One should be added.
     # The solution here is designed to be backwards compatible
     # with previous Python versions, e.g. 1.5.2.
     # XXX This dynamic test should be done only once.
     if getattr(re, "engine", "pre") == "pre":
         return re.pcre_expand(m, template)
     else: # sre
         # XXX This import should be avoidable...
         import sre_parse
         # XXX This parses the template over and over...
         ptemplate = sre_parse.parse_template(template, m.re)
         return sre_parse.expand_template(ptemplate, m)
开发者ID:alexei-matveev,项目名称:ccp1gui,代码行数:15,代码来源:ReplaceDialog.py

示例11: _compiledReplacement

 def _compiledReplacement(self, regexField, replField):
     repl = getattr(self, replField, None)
     if not repl: return BLANK_PARSE_TEMPLATE
     cache = self._compiledCache()
     regex = getattr(self, regexField, None)
     if replField in cache and (regex,repl) in cache[replField]:
         return cache[replField][(regex,repl)]
     else:
         try:
             compiled = parse_template(repl, self._compiledRegex(regexField))
             cache[replField] = {(regex,repl) : compiled}
             return compiled
         except:
             log.warn("Invalid %s on %s", replField, self)
             cache[replField] = {(regex,repl) : None}
             return None
开发者ID:bbc,项目名称:zenoss-prodbin,代码行数:16,代码来源:OSProcessMatcher.py

示例12: setup_template

 def setup_template(self, pattern, template):
     if isinstance(template, compat.binary_type):
         self.binary = True
         tokens = btokens
         ctokens = ctok.btokens
     else:
         self.binary = False
         tokens = utokens
         ctokens = ctok.utokens
     self._original = template
     self._back_ref = set()
     self._def_back_ref = tokens["def_back_ref"]
     self._b_slash = ctokens["b_slash"]
     self._empty = ctokens["empty"]
     self._add_back_references(ctokens["replace_tokens"])
     self._template = self._escape_template(template)
     self.groups, self.literals = sre_parse.parse_template(self._template, pattern)
开发者ID:lupescu,项目名称:subtxt_pkg,代码行数:17,代码来源:bre.py

示例13: __init__

    def __init__(self, pattern, template):
        """Initialize."""

        if isinstance(template, binary_type):
            self.binary = True
            tokens = btokens
        else:
            self.binary = False
            tokens = utokens
        self._original = template
        self._back_ref = set()
        self._b_slash = tokens[_B_SLASH]
        self._def_back_ref = tokens[_DEF_BACK_REF]
        self._empty = tokens[_EMPTY]
        self._add_back_references(tokens[_REPLACE_TOKENS])
        self._template = self.__escape_template(template)
        self.groups, self.literals = sre_parse.parse_template(self._template, pattern)
开发者ID:paregonta,项目名称:BracketHighlighter,代码行数:17,代码来源:__init__.py

示例14: setInfo

    def setInfo(self, **data):
        """
        Set attributes on a process.
        This method accepts any keyword argument for the property that you wish
        to set. The only required property is "uid".

        @type    uid: string
        @keyword uid: Unique identifier of a process
        @rtype:   DirectResponse
        @return:  B{Properties}
            - data: (dictionary) Object representing a process's new properties
        """
        facade = self._getFacade()
        processUid = data['uid']
        for regexParam in ['includeRegex', 'excludeRegex', 'replaceRegex']:
            regex = data.get(regexParam)
            if regex:
                try:
                    re.compile(regex)
                except re.error as e:
                    m = "%s : %s" % (regexParam, e)
                    return DirectResponse.fail(msg=m)
        replaceRegex = data.get('replaceRegex')
        if replaceRegex:
            replaceRegex = re.compile(replaceRegex)
            replacement = data.get('replacement')
            if replacement:
                try:
                    groups, literals = parse_template(replacement,replaceRegex)
                    for index, group in groups:
                        if group > replaceRegex.groups:
                            m = "Group (%s) referenced in replacement must be defined in replaceRegex" % group
                            return DirectResponse.fail(msg=m)
                except re.error as e:
                    m = "replacement : %s" % (e,)
                    return DirectResponse.fail(msg=m)

        process = facade.getInfo(processUid)
        audit('UI.Process.Edit', processUid, data_=data, skipFields_=('uid'))
        return DirectResponse.succeed(data=Zuul.unmarshal(data, process))
开发者ID:bbc,项目名称:zenoss-prodbin,代码行数:40,代码来源:process.py

示例15: expand_sub

def expand_sub(string, template, debug=0, mode='all') :
    """ Given a regular expression and a replacement string, generate expansions of
        the regular expression and for each one return it and its transformation
        as applied by the replacement string.

        string : regular expression to expand
        template : transformation to apply to each regular expression
        mode : can take 3 values
            all : return all possible shortest strings that the regular expression
                    would match
            first : return the first string that all would return
            random : return one random string that the regular expression would match
    """
    pattern = sre_parse.parse(string, flags=sre_parse.SRE_FLAG_VERBOSE)
    pattern.mode = mode
    template = sre_parse.parse_template(template, pattern)
    if debug :
        print pattern
        print template
    for s in _iterate(pattern, pattern.data, MatchObj(pattern, "")) :
        s.patient = 0
        yield (s.string, sre_parse.expand_template(template, s))
开发者ID:HughP,项目名称:Palaso-Python,代码行数:22,代码来源:reggen.py


注:本文中的sre_parse.parse_template函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。