我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用re._pattern_type()。
def filter_var_list(self, var_list): """Filter checkpoint vars for those to be restored. Args: checkpoint_vars (list): Vars that can be restored from checkpoint. to_restore (list[str] or regex, optional): Selects vars to restore. Returns: list: Variables to be restored from checkpoint. """ if not self.to_restore: return var_list elif isinstance(self.to_restore, re._pattern_type): return {name: var for name, var in var_list.items() if self.to_restore.match(name)} elif isinstance(self.to_restore, list): return {name: var for name, var in var_list.items() if name in self.to_restore} raise TypeError('to_restore ({}) unsupported.'.format(type(self.to_restore)))
def __init__(self, pattern): # type: (Union[Text, regex._pattern_type, re._pattern_type]) -> None """ :param pattern: String pattern, or pre-compiled regex. IMPORTANT: If you specify your own compiled regex, be sure to add the ``UNICODE`` flag for Unicode support! """ super(Regex, self).__init__() self.regex = ( pattern if isinstance(pattern, (regex._pattern_type, re._pattern_type)) else regex.compile(pattern, regex.UNICODE) )
def __init__(self, pattern, keys=None): # type: (Union[Text, regex._pattern_type, re._pattern_type], Optional[Sequence[Text]]) -> None """ :param pattern: Regex used to split incoming string values. IMPORTANT: If you specify your own compiled regex, be sure to add the ``UNICODE`` flag for Unicode support! :param keys: If set, the resulting list will be converted into an OrderedDict, using the specified keys. IMPORTANT: If ``keys`` is set, the split value's length must be less than or equal to ``len(keys)``. """ super(Split, self).__init__() self.regex = ( pattern if isinstance(pattern, (regex._pattern_type, re._pattern_type)) else regex.compile(pattern, regex.UNICODE) ) self.keys = keys
def _filter_nodes(self, cond, nodes=None): if nodes is None: nodes = self.nodes res = [] for n in nodes: match = True for k, v in cond.iteritems(): attr = getattr(n, k) if isinstance(v, re._pattern_type) and \ isinstance(attr, basestring) and v.match(attr) is None: match = False break elif attr != v: match = False break if match: res.append(n) return res
def _js_select_by(self, term, number): if isinstance(term, re._pattern_type): js_rx = term.pattern js_rx = js_rx.replace('\\A', '^', 1) js_rx = js_rx.replace('\\Z', '$', 1) js_rx = js_rx.replace('\\z', '$', 1) js_rx = re.sub(r'\(\?#.+\)', '', js_rx) js_rx = re.sub(r'\(\?-\w+:', '(', js_rx) elif type(term) in [six.text_type, six.binary_type]: js_rx = '^{}$'.format(term) else: raise TypeError('expected String or Regexp, got {}'.format(term)) for way in ['text', 'label', 'value']: self._element_call(lambda: self._execute_js('selectOptions{}'.format(way.capitalize()), self, js_rx, str(number))) if self._is_matching_option(way, term): return self.selected_options[0].text raise NoValueFoundException('{} not found in select list'.format(term))
def _xpath_adjacent(self, **kwargs): from .elements.html_elements import HTMLElement, HTMLElementCollection import nerodia plural = kwargs.pop('plural', None) index = kwargs.pop('index', None) tag_name = kwargs.get('tag_name') if not (plural or any(isinstance(val, re._pattern_type) for val in kwargs.values())): kwargs['index'] = index or 0 if not plural and tag_name: klass = nerodia.tag_to_class.get(tag_name) elif not plural: klass = HTMLElement elif tag_name: klass = nerodia.tag_to_class.get('{}_collection'.format(tag_name)) else: klass = HTMLElementCollection return klass(self, kwargs)
def _build_wd_selector(self, selectors): if any(isinstance(val, re._pattern_type) for val in selectors.values()): return None if not selectors.pop('tag_name', None): raise Error('internal error: no tag_name?!') button_attr_exp = self.xpath_builder.attribute_expression('button', selectors) xpath = './/button' if button_attr_exp: xpath += '[{}]'.format(button_attr_exp) if selectors.get('type') is not False: if selectors.get('type') in [None, True]: selectors['type'] = Button.VALID_TYPES input_attr_exp = self.xpath_builder.attribute_expression('input', selectors) xpath += ' | .//input' xpath += '[{}]'.format(input_attr_exp) logging.debug({'build_wd_selector': xpath}) return ['xpath', xpath]
def _build_wd_selector(self, selectors): if any(isinstance(val, re._pattern_type) for val in selectors.values()): return None selectors.pop('tag_name', None) input_attr_exp = self.xpath_builder.attribute_expression('input', selectors) xpath = './/input[(not(@type) or ({}))'.format(self._negative_type_expr) if input_attr_exp: xpath += ' and {}'.format(input_attr_exp) xpath += ']' logging.debug({'build_wd_selector': xpath}) return ['xpath', xpath]
def check_type(self, how, what): if how == 'index': if not isinstance(what, int): raise TypeError('expected {}, got {!r}:{}'.format(int, what, what.__class__)) elif how == 'visible': if not isinstance(what, bool): raise TypeError('expected {}, got {!r}:{}'.format(bool, what, what.__class__)) elif how == 'visible_text': if type(what) not in [six.text_type, six.binary_type, re._pattern_type]: raise TypeError('expected str or regexp, got {}') else: if isinstance(what, list) and how != 'class_name': raise TypeError("only 'class_name' locator can have a value of a list") if type(what) not in self.VALID_WHATS: raise TypeError( 'expected one of {}, got {!r}:{}'.format(self.VALID_WHATS, what, what.__class__))
def _node_match_query(self, node, identifier, *args, **kwargs): if not self._attribute_match_query(node.generate_identifiers(), identifier.lower() if isinstance(identifier, string_instance) and not identifier.startswith( "re:") else identifier): return False all_my_keys = node._str_keys + node._list_keys + node._dict_keys if args and isinstance(args[0], (string_instance, re._pattern_type, list, tuple)): if not self._attribute_match_query([getattr(node, node._default_test_value)], args[0]): return False args = args[1:] for arg in args: if not arg(node): return False for key, value in kwargs.items(): if key not in all_my_keys: return False if not self._attribute_match_query([getattr(node, key)], value): return False return True
def _process_step(self, step): """Steps can be plain strings or tuples when matching images""" if isinstance(step, VersionPick): # Version pick passed, coerce it ... step = step.pick(self.browser.product_version) if isinstance(step, tuple): image = step[0] step = step[1] if isinstance(step, VersionPick): # Version pick passed, coerce it ... step = step.pick(self.browser.product_version) else: image = None if not isinstance(step, six.string_types + (re._pattern_type,)): step = str(step) return image, step
def validate_node(self, node, matcher, image): """Helper method that matches nodes by given conditions. Args: node: Node that is matched matcher: If it is an instance of regular expression, that one is used, otherwise equality comparison is used. Against item name. image: If not None, then after the matcher matches, this will do an additional check for the image name Returns: A :py:class:`bool` if the node is correct or not. """ text = self.browser.text(node) if isinstance(matcher, re._pattern_type): match = matcher.match(text) is not None else: match = matcher == text if not match: return False if image is not None and self.image_getter(node) != image: return False return True
def __init__(self, re_pattern=None, source=None): # ?? ??? ??? re_pattern??? # re._pattern_type? ???? # (???? ????? ?? ??) # self.pattern_str??? re_pattern.pattern?? ?? if isinstance(re_pattern, re._pattern_type): self.pattern_str = re_pattern.pattern # re_pattern??? str(???)?? ?? # self.pattern_str??? ?? ?? ??? ?? elif isinstance(re_pattern, str): self.pattern_str = re_pattern self.source = source
def extract_phenocode_from_filepath(phenolist, regex): print("NOTE: working with {} phenos".format(len(phenolist))) if not isinstance(regex, re._pattern_type): regex = re.compile(regex) for pheno in phenolist: if 'assoc_files' not in pheno: raise PheWebError("ERROR: At least one phenotype doesn't have the key 'assoc_files'.") if not pheno['assoc_files']: raise PheWebError("ERROR: At least one phenotype has an empty 'assoc_files' list.") phenocodes = [] for assoc_filepath in pheno['assoc_files']: match = re.search(regex, assoc_filepath) if match is None: raise PheWebError("ERROR: The regex {!r} doesn't match the filepath {!r}".format(regex.pattern, assoc_filepath)) groups = match.groups() if len(groups) != 1: raise PheWebError("ERROR: The regex {!r} doesn't capture any groups on the filepath {!r}! You're using parentheses without backslashes, right?".format(regex.pattern, assoc_filepath)) phenocodes.append(groups[0]) if len(set(phenocodes)) != 1: raise PheWebError("ERROR: At least one phenotype gets multiple different phenocodes from its several association filepaths. Here they are: {!r}".format(list(set(phenocodes)))) if 'phenocode' in pheno: if pheno['phenocode'] != phenocodes[0]: raise PheWebError("""\ ERROR: The regex {!r} matched the filepaths {!r} to produce the phenocode {!r}. But that phenotype already had a phenocode, {!r}. """.format(regex.pattern, pheno['assoc_files'], phenocodes[0], pheno['phenocode'])) pheno['phenocode'] = phenocodes[0] return phenolist
def process_test_data(raw_data: list, previous_results: dict) -> Dict[str, Dict[str, object]]: leaks = [] result = {} url = None if 'url' in raw_data: url = raw_data['url']['data'].decode() for trial, pattern in TRIALS: if url: if callable(trial): trial = trial(url) if trial is None: continue if trial not in raw_data: # Test raw data too old or particular request failed. continue response = json.loads(raw_data[trial]['data'].decode()) if response['status_code'] == 200: # The pattern can have three different types. # - If it is a simple string, we only check if it is contained in the response if isinstance(pattern, str): if pattern in response['text']: leaks.append(trial) # - If it is a RegEx object, we perform a pattern match elif isinstance(pattern, re._pattern_type): if re.match(response['text']): leaks.append(trial) # - If it is callable, we call it with the response text and check the return value elif callable(pattern): if pattern(response['text']): leaks.append(trial) result['leaks'] = leaks return result
def match_text(text, tag): if isinstance(text, string_types): return text in tag.text if isinstance(text, re._pattern_type): return text.search(tag.text)
def _regex_to_string(a): if isinstance(a, str): return a elif isinstance(a, re._pattern_type): return a.pattern elif isinstance(a, RegexSet): if a.pattern is not None: return a.pattern else: return None else: raise AttributeSetError("Cannot convert regex to string!")
def _has_url_match(self, match, request_url): url = match['url'] if _is_string(url): if match['match_querystring']: return self._has_strict_url_match(url, request_url) else: url_without_qs = request_url.split('?', 1)[0] return url == url_without_qs elif isinstance(url, re._pattern_type) and url.match(request_url): return True else: return False
def _find_options(self, how, term): types = [six.text_type, six.binary_type, int, re._pattern_type] found = [] def func(sel): if type(term) in types: collection = sel.options(value=term) if how == 'value' else [] if not list(collection): collection = sel.options(text=term) if not list(collection): collection = sel.options(label=term) if collection: found.append(collection) return False else: return not found and nerodia.relaxed_locate else: raise TypeError('expected {!r}, got {}:{}'.format(types, term, term.__class__)) try: Wait.until_not(func, object=self) if found: return found[0] raise NoValueFoundException('{} not found in select list'.format(term)) except TimeoutError: raise NoValueFoundException('{} not found in select list'.format(term))
def _build_wd_selector(self, selectors): if any(isinstance(val, re._pattern_type) for val in selectors.values()): return None expressions = ['./th', './td'] attr_expr = self.xpath_builder.attribute_expression(None, selectors) if attr_expr: expressions = ['{}[{}]'.format(e, attr_expr) for e in expressions] xpath = " | ".join(expressions) logging.debug({'build_wd_selector': xpath}) return ['xpath', xpath]
def match_str_or_regex(str_or_regex, term): if isinstance(str_or_regex, re._pattern_type) and str_or_regex.search(term): return True elif str_or_regex == term: return True else: return False
def _delete_regexps_from(selector): rx_selector = {} for how, what in copy(selector).items(): if not isinstance(what, re._pattern_type): continue rx_selector[how] = what selector.pop(how) return rx_selector
def _build_wd_selector(self, selectors): if any(isinstance(val, re._pattern_type) for val in selectors.values()): return None return self._build_xpath(selectors)
def regex_compile(pattern, flags=0): if not isinstance(pattern, re._pattern_type): return re.compile(pattern, flags=flags) return pattern
def find_command(self, command): """Return an iter of jobs matching any part of the command.""" for job in list(self.crons): if isinstance(command, re._pattern_type): if command.findall(job.command): yield job elif command in job.command: yield job
def find_comment(self, comment): """Return an iter of jobs that match the comment field exactly.""" for job in list(self.crons): if isinstance(comment, re._pattern_type): if comment.findall(job.comment): yield job elif comment == job.comment: yield job
def _attribute_match_query(self, attribute_names, query): """ Take a list/tuple of attributes that can match and a query, return True if any of the attributes match the query. """ assert isinstance(attribute_names, (list, tuple)) if isinstance(query, string_instance) and query.startswith("re:"): query = re.compile(query[3:]) for attribute in attribute_names: if callable(query): if query(attribute): return True elif isinstance(query, string_instance) and query.startswith("g:"): if fnmatch(attribute, query[2:]): return True elif isinstance(query, re._pattern_type): if query.match(attribute): return True elif isinstance(query, (list, tuple)): if attribute in query: return True else: if attribute == query: return True return False
def test_compile(self): # Test return value when given string and pattern as parameter pattern = re.compile('random pattern') self.assertIsInstance(pattern, re._pattern_type) same_pattern = re.compile(pattern) self.assertIsInstance(same_pattern, re._pattern_type) self.assertIs(same_pattern, pattern) # Test behaviour when not given a string or pattern as parameter self.assertRaises(TypeError, re.compile, 0)
def register_handler(self, pattern, method): if not isinstance(pattern, re._pattern_type): pattern = re.compile(pattern) self._handlers[pattern] = method
def register_dm_handler(self, pattern, method): if not isinstance(pattern, re._pattern_type): pattern = re.compile(pattern) self._dm_handlers[pattern] = method
def is_re(obj): return isinstance(obj, re._pattern_type)
def _normalizeDirPattern(pattern, path): bIsRegex = False tmpPattern = pattern if (isinstance(pattern, re._pattern_type)): tmpPattern = pattern.pattern bIsRegex = True elif (pattern.startswith('re:')): tmpPattern = pattern[3:] bIsRegex = True numPathSep = path.count(os.path.sep) numPatternSep = tmpPattern.count(os.path.sep) # When the path has more levels, fill in the pattern with wildcards if (numPathSep > numPatternSep): while (numPathSep > numPatternSep): if (bIsRegex): if (tmpPattern != ''): tmpPattern = tmpPattern + "/.*" else: tmpPattern = '.*' else: tmpPattern = os.path.join(tmpPattern, "*") numPatternSep = numPatternSep + 1 if (bIsRegex): return re.compile(tmpPattern) else: return tmpPattern
def _normalizeFilePattern(pattern, filepath): bIsRegex = False tmpPattern = pattern if (isinstance(pattern, re._pattern_type)): tmpPattern = pattern.pattern bIsRegex = True elif (pattern.startswith('re:')): tmpPattern = pattern[3:] bIsRegex = True # Separate the file pattern from the dir/path pattern patternParts = os.path.split(tmpPattern) tmpPattern = patternParts[0] numPathSep = filepath.count(os.path.sep) numPatternSep = tmpPattern.count(os.path.sep) if (tmpPattern != ''): numPatternSep = numPatternSep + 1 # When the path has more levels, fill in the pattern with wildcards if (numPathSep > numPatternSep): while (numPathSep > numPatternSep): if (bIsRegex): if (tmpPattern != ''): tmpPattern = tmpPattern + "/.*" else: tmpPattern = '.*' else: tmpPattern = os.path.join(tmpPattern, "*") numPatternSep = numPatternSep + 1 # Append the file pattern back if (bIsRegex): tmpPattern = tmpPattern + "/" + patternParts[1] else: tmpPattern = os.path.join(tmpPattern, patternParts[1]) if (bIsRegex): return re.compile(tmpPattern) else: return tmpPattern
def zdb_like_op(left, right, c, compiler, tables, format_args): r"""Implement the ``LIKE`` operator. In a normal context, produces the expression:: column:"foo" E.g.:: stmt = select([sometable]).\ where(sometable.c.column.like("foo")) In a regex context, produces the expression:: column:~"foo[a-z]" E.g.:: stmt = select([sometable]).\ where(sometable.c.column.like(re.compile("foo[a-z]"))) """ from sqlalchemy_zdb.compiler import compile_clause if isinstance(right.value, re._pattern_type): _oper = ":~" else: _oper = ":" return "%s%s%s" % (left.name, _oper, compile_clause(right, compiler, tables, format_args))
def compile_clause(c, compiler, tables, format_args): if isinstance(c, BindParameter) and isinstance(c.value, ( str, int, re._pattern_type, ZdbLiteral)): if isinstance(c.value, str): return "\"%s\"" % escape_tokens(c.value) elif isinstance(c.value, re._pattern_type): return "\"%s\"" % c.value.pattern elif isinstance(c.value, ZdbLiteral): return c.value.literal else: return c.value elif isinstance(c, (True_, False_)): return str(type(c) == True_).lower() elif isinstance(c, TextClause): return c.text elif isinstance(c, BinaryExpression): return compile_binary_clause(c, compiler, tables, format_args) elif isinstance(c, BooleanClauseList): return compile_boolean_clause_list(c, compiler, tables, format_args) elif isinstance(c, Column): return compile_column_clause(c, compiler, tables, format_args) elif isinstance(c, Grouping): return compile_grouping(c, compiler, tables, format_args) elif isinstance(c, Null): return "NULL" raise ValueError("Unsupported clause")
def register_handler(self, handler, regex, group): if not isinstance(regex, re._pattern_type): regex = re.compile(regex) self.handlers.append((handler, regex, group))
def __init__(self, indent=1, contents=None, ignore_lines=None): self._indent = indent self._items = list() self._config_text = None if ignore_lines: for item in ignore_lines: if not isinstance(item, re._pattern_type): item = re.compile(item) DEFAULT_IGNORE_LINES_RE.add(item) if contents: self.load(contents)
def _repr_step(image, step): if isinstance(step, re._pattern_type): # Make it look like r'pattern' step_repr = 'r' + re.sub(r'^[^"\']', '', repr(step.pattern)) else: step_repr = step if image is None: return step_repr else: return '{}[{}]'.format(step_repr, image)
def test_regex_string(self): parser = futhon_parser.FuthonParser() self.assertIsInstance(parser.parse("#\"\""), re._pattern_type) self.assertIsInstance(parser.parse("#\"hello\""), re._pattern_type)