本文整理汇总了Python中re.MatchObject方法的典型用法代码示例。如果您正苦于以下问题:Python re.MatchObject方法的具体用法?Python re.MatchObject怎么用?Python re.MatchObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类re
的用法示例。
在下文中一共展示了re.MatchObject方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _handle_script_request
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def _handle_script_request(self,
environ,
start_response,
url_map,
match,
inst=None):
"""Handles a HTTP request that has matched a script handler.
Args:
environ: An environ dict for the request as defined in PEP-333.
start_response: A function with semantics defined in PEP-333.
url_map: An appinfo.URLMap instance containing the configuration for the
handler that matched.
match: A re.MatchObject containing the result of the matched URL pattern.
inst: The Instance to send the request to. If None then an appropriate
Instance will be chosen.
Returns:
An iterable over strings containing the body of the HTTP response.
"""
raise NotImplementedError()
示例2: handle
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def handle(self, match, environ, start_response):
"""Serves the file content matching the request.
Args:
match: The re.MatchObject containing the result of matching the URL
against this handler's URL pattern.
environ: An environ dict for the current request as defined in PEP-333.
start_response: A function with semantics defined in PEP-333.
Returns:
An iterable over strings containing the body of the HTTP response.
"""
full_path = os.path.join(self._root_path,
self._url_map.static_dir,
match.group('file'))
return self._handle_path(full_path, environ, start_response)
示例3: populate
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def populate(self, context, match):
"""
Utilizes the match from the regular expression check to check for
possible matches of :py:attr:`.jinja_vars`.
:param context: The context for the completion.
:type context: :py:class:`GtkSource.CompletionContext`
:param match: The matching object.
:types match: `re.MatchObject`
:return: List of strings to be used for creation of proposals.
:rtype: list
"""
proposal_terms = []
if match.group('is_filter'):
jinja_filter = match.group('filter') or ''
proposal_terms = [term for term in self.jinja_filters if term.startswith(jinja_filter)]
elif match.group('is_test'):
jinja_test = match.group('test') or ''
proposal_terms = [term for term in self.jinja_tests if term.startswith(jinja_test)]
elif match.group('var'):
tokens = match.group('var')
tokens = tokens.split('.')
proposal_terms = get_proposal_terms(self.jinja_tokens, tokens)
proposal_terms = [(term.split('(', 1)[0], term) for term in proposal_terms]
return proposal_terms
示例4: _StringEscape
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def _StringEscape(self, string='', match='', **unused_kwargs):
"""Escapes backslashes found inside an expression string.
Backslashes followed by anything other than [\\'"rnbt.ws] will raise
an Error.
Note that this function is used as a callback by _GetNextToken.
Args:
string (Optional[str]): expression string.
match (Optional[re.MatchObject]): the regular expression match object,
where match.group(1) contains the escaped code.
Returns:
str: next state, which is None.
Raises:
ParseError: when the escaped string is not one of [\\'"rnbt].
"""
if match.group(1) not in '\\\'"rnbt\\.ws':
raise errors.ParseError('Invalid escape character {0:s}.'.format(string))
decoded_string = codecs.decode(string, 'unicode_escape')
return self._StringInsert(string=decoded_string)
示例5: infix_matches
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def infix_matches(self, substring: str) -> List[Match]:
"""Find internal split points of the string, such as hyphens.
Args:
substring : The string to segment.
Returns:
A list of `re.MatchObject` objects that have `.start()`
and `.end()` methods, denoting the placement of internal
segment separators, e.g. hyphens.
"""
# Return empty list if no infix matches are in substring.
if self.infix_finditer is None:
return []
# Return a list of MatchObject instances over all non-overlapping
# matches for the infixes in the substring.
return list(self.infix_finditer(substring))
示例6: find_prefix
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def find_prefix(self, substring: str) -> int:
"""Find the length of a prefix that should be segmented from the
string, or None if no prefix rules match.
Args:
substring: The string to segment.
Returns:
The length of the prefix if present, otherwise 0.
"""
# Return 0 if no prefix match is found in substring.
if self.prefix_search is None:
return 0
# The MatchObject with the end and start postion of the prefix in the substring.
match = self.prefix_search(substring)
# Return the length of the prefix match in the substring.
return (match.end() - match.start()) if match is not None else 0
示例7: find_suffix
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def find_suffix(self, substring: str) -> int:
"""Find the length of a suffix that should be segmented from the
string, or None if no suffix rules match.
Args:
substring: The string to segment.
Returns:
The length of the suffix if present, otherwise 0.
"""
# Return 0 if no suffix match is found in substring.
if self.suffix_search is None:
return 0
# The MatchObject with the end and start postion of the suffix in the substring.
match = self.suffix_search(substring)
# Return the length of the suffix match in the substring.
return (match.end() - match.start()) if match is not None else 0
示例8: handle
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def handle(self, match, environ, start_response):
"""Serves the file content matching the request.
Args:
match: The re.MatchObject containing the result of matching the URL
against this handler's URL pattern.
environ: An environ dict for the current request as defined in PEP-333.
start_response: A function with semantics defined in PEP-333.
Returns:
An iterable over strings containing the body of the HTTP response.
"""
relative_path = match.expand(self._url_map.static_files)
if not self._is_relative_path_valid(relative_path):
if self._url_map.require_matching_file:
return None
else:
return self._not_found_404(environ, start_response)
full_path = os.path.join(self._root_path, relative_path)
return self._handle_path(full_path, environ, start_response)
示例9: __init__
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def __init__ (self, result=None) :
"""
construct with a `re.MatchObject` instance. This ctor should only be
called from within the `ReString` class.
"""
self._glist = list()
self._gdict = dict()
if result :
if not isinstance (result, type(re.match("",""))) : # fuck python
raise TypeError ('Need re.MatchObject on construction, not %s' % type(result))
self._glist = result.groups ()
self._gdict = result.groupdict ()
# --------------------------------------------------------------------------
#
示例10: handle
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def handle(self, environ, start_response, url_map, match, request_id,
request_type):
"""Serves a request by displaying an error page.
Args:
environ: An environ dict for the request as defined in PEP-333.
start_response: A function with semantics defined in PEP-333.
url_map: An appinfo.URLMap instance containing the configuration for the
handler matching this request.
match: A re.MatchObject containing the result of the matched URL pattern.
request_id: A unique string id associated with the request.
request_type: The type of the request. See instance.*_REQUEST module
constants.
Yields:
A sequence of strings containing the body of the HTTP response.
"""
start_response('500 Internal Server Error',
[('Content-Type', 'text/html')])
yield '<html><head><title>Invalid PHP Configuration</title></head>'
yield '<body>'
yield '<title>Invalid PHP Configuration</title>'
yield '<b>The PHP interpreter specified with the --php_executable_path flag'
yield ' ("%s") is not compatible with the App Engine PHP ' % (
self._php_executable_path)
yield 'development environment.</b><br>'
yield '<br>'
yield '<pre>%s</pre>' % self._problem_description
yield '</body></html>'
示例11: _handle_instance_request
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def _handle_instance_request(self,
environ,
start_response,
url_map,
match,
request_id,
inst,
request_type):
"""Handles a request routed a particular Instance.
Args:
environ: An environ dict for the request as defined in PEP-333.
start_response: A function with semantics defined in PEP-333.
url_map: An appinfo.URLMap instance containing the configuration for the
handler that matched.
match: A re.MatchObject containing the result of the matched URL pattern.
request_id: A unique string id associated with the request.
inst: The instance.Instance to send the request to.
request_type: The type of the request. See instance.*_REQUEST module
constants.
Returns:
An iterable over strings containing the body of the HTTP response.
"""
if request_type != instance.READY_REQUEST:
with self._condition:
self._num_outstanding_instance_requests += 1
self._outstanding_request_history.append(
(time.time(), self.num_outstanding_instance_requests))
try:
logging.debug('Dispatching request to %s', inst)
return inst.handle(environ, start_response, url_map, match, request_id,
request_type)
finally:
with self._condition:
if request_type != instance.READY_REQUEST:
self._num_outstanding_instance_requests -= 1
self._condition.notify()
示例12: extract
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def extract(self, context):
"""
Used to extract the text according to the :py:attr:`.left_delimiter` and
:py:attr:`.extraction_regex`. If the extraction regular expression does
not match, None is returned.
:param context: The context for the completion.
:type context: :py:class:`GtkSource.CompletionContext`
:return: The resulting match from the :py:attr:`.extraction_regex`.
:rtype: :py:class:`re.MatchObject`
"""
end_iter = context.get_iter()
if not isinstance(end_iter, Gtk.TextIter):
_, end_iter = context.get_iter()
if not end_iter:
return
buf = end_iter.get_buffer()
mov_iter = end_iter.copy()
limit_iter = end_iter.copy()
if self.left_limit:
limit_iter.backward_chars(self.left_limit)
mov_iter = mov_iter.backward_search(self.left_delimiter, Gtk.TextSearchFlags.VISIBLE_ONLY, limit=limit_iter)
if not mov_iter:
return
mov_iter, _ = mov_iter
if self.left_delimiter_adjustment > 0:
mov_iter.forward_chars(self.left_delimiter_adjustment)
elif self.left_delimiter_adjustment < 0:
mov_iter.backward_chars(abs(self.left_delimiter_adjustment))
left_text = buf.get_text(mov_iter, end_iter, True)
return self.extraction_regex.match(left_text)
示例13: match_regex
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def match_regex(self, regex):
"""Attempt to match a regular expression against the error.
Args:
regex: The regular expression to match against
Returns:
re.MatchObject or None
"""
return re.search(regex, self.__run_response.error, re.MULTILINE)
示例14: _read_until
# 需要导入模块: import re [as 别名]
# 或者: from re import MatchObject [as 别名]
def _read_until(term, pattern, timeout):
"""
Convenience read-until-pattern function, supporting :meth:`~.get_location`.
:arg blessed.Terminal term: :class:`~.Terminal` instance.
:arg float timeout: timeout period, may be set to None to indicate no
timeout (where 0 is always returned).
:arg str pattern: target regular expression pattern to seek.
:rtype: tuple
:returns: tuple in form of ``(match, str)``, *match*
may be :class:`re.MatchObject` if pattern is discovered
in input stream before timeout has elapsed, otherwise
None. ``str`` is any remaining text received exclusive
of the matching pattern).
The reason a tuple containing non-matching data is returned, is that the
consumer should push such data back into the input buffer by
:meth:`~.Terminal.ungetch` if any was received.
For example, when a user is performing rapid input keystrokes while its
terminal emulator surreptitiously responds to this in-band sequence, we
must ensure any such keyboard data is well-received by the next call to
term.inkey() without delay.
"""
stime = time.time()
match, buf = None, u''
# first, buffer all pending data. pexpect library provides a
# 'searchwindowsize' attribute that limits this memory region. We're not
# concerned about OOM conditions: only (human) keyboard input and terminal
# response sequences are expected.
while True:
# block as long as necessary to ensure at least one character is
# received on input or remaining timeout has elapsed.
ucs = term.inkey(timeout=_time_left(stime, timeout))
if ucs:
buf += ucs
# while the keyboard buffer is "hot" (has input), we continue to
# aggregate all awaiting data. We do this to ensure slow I/O
# calls do not unnecessarily give up within the first 'while' loop
# for short timeout periods.
while True:
ucs = term.inkey(timeout=0)
if not ucs:
break
buf += ucs
match = re.search(pattern=pattern, string=buf)
if match is not None:
# match
break
if timeout is not None:
if not _time_left(stime, timeout):
# timeout
break
return match, buf