我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用typing.Match()。
def translate_non_sgml_chars(data, enc='utf-8'): # type: (bytes, str) -> bytes def replace_non_sgml(m): # type: (Match) -> str codepoint = ord(m.group(0)) if 127 <= codepoint <= 159: try: return int2byte(codepoint).decode('windows-1252') except UnicodeDecodeError: pass # Unicode Character 'REPLACEMENT CHARACTER' return u'\ufffd' text = data.decode(enc, 'replace') text = re.sub(unistr(r'[\x00-\x08\x0b\x0c\x0e-\x1f\x7f-\x9f]'), replace_non_sgml, text) return text.encode(enc, 'replace')
def unescape(text: str) -> str: def fixup(m: typing.Match): in_text = m.group(0) if in_text[:2] == "&#": # character reference try: if in_text[:3] == "&#x": return chr(int(in_text[3:-1], 16)) else: return chr(int(in_text[2:-1])) except ValueError: pass else: # named entity try: in_text = chr(html.entities.name2codepoint[in_text[1:-1]]) except KeyError: pass return in_text # leave as is return re.sub("&#?\w+;", fixup, text)
def ill_cp_escaper(m): # type: (Match) -> str codepoint = ord(m.group(0)) if codepoint < 0x100: return u"\\x%02x" % codepoint elif codepoint < 0x10000: return u"\\u%04x" % codepoint else: return u"\\U%06x" % codepoint
def ill_cp_unescaper(m): # type: (Match[str]) -> str return unichr(int(m.group(1)[1:], 16))
def __init__(self, username: str, message: str, player: Player, args: List[Match[str]]): """ Initializes a new CommandSentEvent :param username: The username of the player that sent the command :param message: The message containing the command :param player: The player that sent the command :param args: The args for the command """ super().__init__(username, message, player) self.args = args
def _lower_dash(m: Match) -> str: return m.group(1) + '-' + m.group(2).lower()
def zfillrepl(matchobj: typing.Match): return matchobj.group(0).zfill(3)
def check_type(value: typing.Any, hint: typing.Optional[type]) -> bool: """Check given ``value``'s type. :param value: given argument :param hint: expected type of given ``value``. as like :mod:`typing` interprets, :const:`None` is interpreted as :class:`types.NoneType` :type hint: :class:`typing.Optional`[:class:`type`] """ if hint is None: hint = NoneType actual_type = type(value) if hint is NoneType: correct = value is None elif hint is typing.Any: correct = True elif hint is typing.Pattern or hint is typing.Match: correct = isinstance(value, hint.impl_type) elif isinstance(hint, typing.TypeVar): # TODO: Check generic correct = True elif issubclass(hint, typing.Callable): actual_type, correct = check_callable(value, hint) elif issubclass(hint, typing.Tuple): actual_type, correct = check_tuple(value, hint) elif issubclass(hint, typing.Union): actual_type, correct = check_union(value, hint) else: correct = isinstance(value, hint) return actual_type, correct
def check_match(a: typing.Match) -> typing.Any: return a
def decode_routing_key(cls, encoded_routing_key: str) -> str: def decode(match: Match) -> str: return binascii.unhexlify(match.group(1).encode('utf-8')).decode('utf-8') return re.sub(r'___([a-f0-9]{2}|[a-f0-9]{4}|[a-f0-9]{6}|[a-f0-9]{8})_', decode, encoded_routing_key)
def encode_routing_key(cls, routing_key: str) -> str: def encode(match: Match) -> str: return '___' + binascii.hexlify(match.group(1).encode('utf-8')).decode('utf-8') + '_' return re.sub(r'([^a-zA-Z0-9_*#,;.:<>!"%&/\(\)\[\]\{\}\\=?\'^`~+|@$ -])', encode, routing_key)
def decode_topic(cls, encoded_topic: str) -> str: def decode(match: Match) -> str: return binascii.unhexlify(match.group(1).encode('utf-8')).decode('utf-8') return re.sub(r'___([a-f0-9]{2}|[a-f0-9]{4}|[a-f0-9]{6}|[a-f0-9]{8})_', decode, encoded_topic)
def encode_topic(cls, topic: str) -> str: def encode(match: Match) -> str: return '___' + binascii.hexlify(match.group(1).encode('utf-8')).decode('utf-8') + '_' return re.sub(r'([^a-zA-Z0-9_*#-])', encode, topic)
def isSafeLiteral(x: str) -> bool: m: Match = re.match("^\w+$", x) m2: Match = re.match("^[^\d]", x) return m is not None and m2 is not None
def match(self, data: str, *a: Any, **kw: Any) -> Either[str, 'Match']: return ( Maybe(self.rex.match(data, *a, **kw)) .to_either('`{}` does not match `{}`'.format(data, self.spec)) / L(Match)(self, _, data) )
def search(self, data: str, *a: Any, **kw: Any) -> Either[str, 'Match']: return ( Maybe(self.rex.search(data, *a, **kw)) .to_either('`{}` does not contain `{}`'.format(data, self.spec)) / L(Match)(self, _, data) )
def __init__(self, regex: Regex, internal: typing.Match, data: str) -> None: self.regex = regex self.internal = internal self.data = data
def __str__(self) -> str: return 'Match({}, {}, {})'.format(self.regex, self.data, self.group_map)
def logs(request: HttpRequest, tool: str = "all") -> HttpResponse: """Logs listing.""" if not request.user.is_staff: return render_error(request, "You need to be an admin to see logs.") log_lines: typing.List[str] = [] if tool == "all" or tool == "webcrawler": f = open(MAIN_LOGGER, 'rt', encoding='utf8') log_lines = f.read().split('[0m\n') f.close() log_lines.pop() log_lines.reverse() log_filter = request.GET.get('filter', '') if log_filter: log_lines = [x for x in log_lines if log_filter.lower() in x.lower()] current_base_uri = re.escape('{scheme}://{host}'.format(scheme=request.scheme, host=request.get_host())) # Build complete URL for relative internal URLs (some) if crawler_settings.urls.viewer_main_url: patterns = [ r'(?!' + current_base_uri + r')/' + crawler_settings.urls.viewer_main_url + r"archive/\d+/?", r'(?!' + current_base_uri + r')/' + crawler_settings.urls.viewer_main_url + r"gallery/\d+/?", r'(?!' + current_base_uri + r')/' + crawler_settings.urls.viewer_main_url + r"wanted-gallery/\d+/?", ] else: patterns = [ r'(?<!' + current_base_uri + r')/archive/\d+/?', r'(?<!' + current_base_uri + r')/gallery/\d+/?', r'(?<!' + current_base_uri + r')/wanted-gallery/\d+/?', ] def build_request(match_obj: typing.Match) -> str: return request.build_absolute_uri(match_obj.group(0)) log_lines = [reduce(lambda v, pattern: re.sub(pattern, build_request, v), patterns, line) for line in log_lines] paginator = Paginator(log_lines, 100) try: page = int(request.GET.get("page", '1')) except ValueError: page = 1 try: log_lines = paginator.page(page) except (InvalidPage, EmptyPage): log_lines = paginator.page(paginator.num_pages) d = {'log_lines': log_lines} return render(request, "viewer/logs.html", d)