Python typing.Pattern() Examples
The following are 30
code examples of typing.Pattern().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
typing
, or try the search function
.
Example #1
Source File: parsing.py From neuralmonkey with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_first_match(pattern: Pattern, string: str) -> str: """Return the first matching substring. Args: pattern: The pattern to find. string: The string to search. Returns: The first occurence of the pattern in the string. Raises: ValueError if the string does not match the pattern. """ match = pattern.match(string) if match is None: raise ValueError("String '{}' does not match the pattern '{}'" .format(string, pattern.pattern)) return match.group(1) # this is a function because of the parse_* # functions which are not defined yet
Example #2
Source File: review.py From nixpkgs-review with MIT License | 6 votes |
def filter_packages( changed_packages: Set[str], specified_packages: Set[str], package_regexes: List[Pattern[str]], ) -> Set[str]: packages: Set[str] = set() if len(specified_packages) == 0 and len(package_regexes) == 0: return changed_packages if len(specified_packages) > 0: packages = join_packages(changed_packages, specified_packages) for attr in changed_packages: for regex in package_regexes: if regex.match(attr): packages.add(attr) return packages
Example #3
Source File: parser.py From appstore with GNU Affero General Public License v3.0 | 6 votes |
def find_app_id(tar: Any, app_folder_regex: Pattern) -> str: """ Finds and returns the app id by looking at the first level folder :raises InvalidAppPackageStructureException: if there is no valid or to many app folders :param tar: the archive :return: the app id """ folders = find_app_folders(tar, app_folder_regex) if len(folders) > 1: msg = 'More than one possible app folder found' raise InvalidAppPackageStructureException(msg) elif len(folders) == 0: msg = 'No possible app folder found. App folder must contain ' \ 'only lowercase ASCII characters or underscores' raise InvalidAppPackageStructureException(msg) return folders.pop()
Example #4
Source File: marks.py From kitty with GNU General Public License v3.0 | 6 votes |
def marker_from_regex(expression: Union[str, Pattern], color: int, flags: int = re.UNICODE) -> MarkerFunc: color = max(1, min(color, 3)) if isinstance(expression, str): pat = re.compile(expression, flags=flags) else: pat = expression def marker(text: str, left_address: int, right_address: int, color_address: int) -> Generator[None, None, None]: left, right, colorv = get_output_variables(left_address, right_address, color_address) colorv.value = color for match in pat.finditer(text): left.value = match.start() right.value = match.end() - 1 yield return marker
Example #5
Source File: review.py From nixpkgs-review with MIT License | 6 votes |
def __init__( self, builddir: Builddir, build_args: str, no_shell: bool, api_token: Optional[str] = None, use_ofborg_eval: Optional[bool] = True, only_packages: Set[str] = set(), package_regexes: List[Pattern[str]] = [], checkout: CheckoutOption = CheckoutOption.MERGE, ) -> None: self.builddir = builddir self.build_args = build_args self.no_shell = no_shell self.github_client = GithubClient(api_token) self.use_ofborg_eval = use_ofborg_eval self.checkout = checkout self.only_packages = only_packages self.package_regex = package_regexes
Example #6
Source File: dead.py From dead with MIT License | 6 votes |
def _filenames( files_re: Pattern[str], exclude_re: Pattern[str], tests_re: Pattern[str], ) -> Generator[Tuple[str, bool], None, None]: # TODO: zsplit is more correct than splitlines out = subprocess.check_output(('git', 'ls-files')).decode() for filename in out.splitlines(): if ( not files_re.search(filename) or exclude_re.search(filename) or not os.path.exists(filename) or 'python' not in tags_from_path(filename) ): continue yield filename, bool(tests_re.search(filename))
Example #7
Source File: model_zoo_tester.py From ngraph-onnx with Apache License 2.0 | 6 votes |
def __init__(self, backend, zoo_models, parent_module=None): # type: (Type[Backend], List[Dict[str,str]], Optional[str]) -> None self.backend = backend self._parent_module = parent_module self._include_patterns = set() # type: Set[Pattern[Text]] self._exclude_patterns = set() # type: Set[Pattern[Text]] self._test_items = defaultdict(dict) # type: Dict[Text, Dict[Text, TestItem]] for zoo_model in zoo_models: test_name = 'test_{}'.format(zoo_model['model_name']) test_case = OnnxTestCase( name=test_name, url=zoo_model['url'], model_name=zoo_model['model_name'], model_dir=None, model=None, data_sets=None, kind='OnnxBackendRealModelTest', rtol=zoo_model.get('rtol', 0.001), atol=zoo_model.get('atol', 1e-07), ) self._add_model_test(test_case, 'Zoo')
Example #8
Source File: parsing.py From neuralmonkey with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_first_match(pattern: Pattern, string: str) -> str: """Return the first matching substring. Args: pattern: The pattern to find. string: The string to search. Returns: The first occurence of the pattern in the string. Raises: ValueError if the string does not match the pattern. """ match = pattern.match(string) if match is None: raise ValueError("String '{}' does not match the pattern '{}'" .format(string, pattern.pattern)) return match.group(1) # this is a function because of the parse_* # functions which are not defined yet
Example #9
Source File: parsing.py From neuralmonkey with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_first_match(pattern: Pattern, string: str) -> str: """Return the first matching substring. Args: pattern: The pattern to find. string: The string to search. Returns: The first occurence of the pattern in the string. Raises: ValueError if the string does not match the pattern. """ match = pattern.match(string) if match is None: raise ValueError("String '{}' does not match the pattern '{}'" .format(string, pattern.pattern)) return match.group(1) # this is a function because of the parse_* # functions which are not defined yet
Example #10
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_errors(self): with self.assertRaises(TypeError): # Doesn't fit AnyStr. Pattern[int] with self.assertRaises(TypeError): # Can't change type vars? Match[T] m = Match[Union[str, bytes]] with self.assertRaises(TypeError): # Too complicated? m[str] with self.assertRaises(TypeError): # We don't support isinstance(). isinstance(42, Pattern) with self.assertRaises(TypeError): # We don't support isinstance(). isinstance(42, Pattern[str])
Example #11
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_basics(self): pat = re.compile('[a-z]+', re.I) assert issubclass(pat.__class__, Pattern) assert issubclass(type(pat), Pattern) assert issubclass(type(pat), Pattern[str]) mat = pat.search('12345abcde.....') assert issubclass(mat.__class__, Match) assert issubclass(mat.__class__, Match[str]) assert issubclass(mat.__class__, Match[bytes]) # Sad but true. assert issubclass(type(mat), Match) assert issubclass(type(mat), Match[str]) p = Pattern[Union[str, bytes]] assert issubclass(Pattern[str], Pattern) assert issubclass(Pattern[str], p) m = Match[Union[bytes, str]] assert issubclass(Match[bytes], Match) assert issubclass(Match[bytes], m)
Example #12
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_any_is_subclass(self): # Any should be considered a subclass of everything. assert issubclass(Any, Any) assert issubclass(Any, typing.List) assert issubclass(Any, typing.List[int]) assert issubclass(Any, typing.List[T]) assert issubclass(Any, typing.Mapping) assert issubclass(Any, typing.Mapping[str, int]) assert issubclass(Any, typing.Mapping[KT, VT]) assert issubclass(Any, Generic) assert issubclass(Any, Generic[T]) assert issubclass(Any, Generic[KT, VT]) assert issubclass(Any, AnyStr) assert issubclass(Any, Union) assert issubclass(Any, Union[int, str]) assert issubclass(Any, typing.Match) assert issubclass(Any, typing.Match[str]) # These expressions must simply not fail. typing.Match[Any] typing.Pattern[Any] typing.IO[Any]
Example #13
Source File: compareDB3.py From armi with Apache License 2.0 | 6 votes |
def _compareTimeStep( out, refGroup, srcGroup, diffResults, exclusions: Optional[Sequence[Pattern]] = None ): groupNames, structDiffs = _compareH5Groups( out, refGroup, srcGroup, "composite objects/auxiliary data" ) diffResults.addStructureDiffs(structDiffs) componentTypes = {gn for gn in groupNames if gn in ArmiObject.TYPES} auxData = set(groupNames) - componentTypes auxData.discard("layout") for componentType in componentTypes: refTypeGroup = refGroup[componentType] srcTypeGroup = srcGroup[componentType] _compareComponentData( out, refTypeGroup, srcTypeGroup, diffResults, exclusions=exclusions ) for aux in auxData: _compareAuxData(out, refGroup[aux], srcGroup[aux], diffResults, exclusions)
Example #14
Source File: GenericCMS.py From comission with GNU General Public License v3.0 | 6 votes |
def get_addon_version( self, addon: Addon, addon_path: str, version_file_regexp: Pattern[str], to_strip: str ) -> str: version = "" try: path = os.path.join(addon_path, addon.filename) with open(path, encoding="utf8") as addon_info: for line in addon_info: version = version_file_regexp.search(line) if version: candidate_version = str(version.group(1).strip(to_strip)) if candidate_version != "VERSION": # Drupal specific addon.version = candidate_version LOGGER.print_cms("default", "Version : " + addon.version, "", 1) break except FileNotFoundError as e: msg = "No standard extension file. Search manually !" LOGGER.print_cms("alert", "[-] " + msg, "", 1) addon.notes = msg return "" return addon.version
Example #15
Source File: recwarn.py From pytest with MIT License | 6 votes |
def __init__( self, expected_warning: Optional[ Union["Type[Warning]", Tuple["Type[Warning]", ...]] ] = None, match_expr: Optional[Union[str, "Pattern"]] = None, ) -> None: super().__init__() msg = "exceptions must be derived from Warning, not %s" if expected_warning is None: expected_warning_tup = None elif isinstance(expected_warning, tuple): for exc in expected_warning: if not issubclass(exc, Warning): raise TypeError(msg % type(exc)) expected_warning_tup = expected_warning elif issubclass(expected_warning, Warning): expected_warning_tup = (expected_warning,) else: raise TypeError(msg % type(expected_warning)) self.expected_warning = expected_warning_tup self.match_expr = match_expr
Example #16
Source File: markdown_extension.py From zulip with Apache License 2.0 | 6 votes |
def extract_code_example(source: List[str], snippet: List[Any], example_regex: Pattern[str]) -> List[Any]: start = -1 end = -1 for line in source: match = example_regex.search(line) if match: if match.group(1) == 'start': start = source.index(line) elif match.group(1) == 'end': end = source.index(line) break if (start == -1 and end == -1): return snippet snippet.append(source[start + 1: end]) source = source[end + 1:] return extract_code_example(source, snippet, example_regex)
Example #17
Source File: marked_up_text.py From lexpredict-contraxsuite with GNU Affero General Public License v3.0 | 5 votes |
def replace_by_regex(self, reg: Pattern, replacement: str, start: Optional[int] = None, end: Optional[int] = None) -> None: """ Replace phrases matching "reg" with "replacement", adjusting labels' coordinates """ if not self.text: return if not self.labels: self.text = reg.sub(replacement, self.text) return repl_len = len(replacement) new_text = '' last_index = 0 transformations = [] text_to_check = self.text if start is None else self.text[start: end] start = start or 0 for match in reg.finditer(text_to_check): match_start = match.start() + start match_end = match.end() + start new_text += self.text[last_index: match_start] new_text += replacement last_index = match_end delta = repl_len - match_end + match_start if delta != 0: transformations.append(((match_start, match_end), (match_start, match_end + delta))) new_text += self.text[last_index:len(self.text)] self.text = new_text self.apply_transformations(transformations)
Example #18
Source File: utils.py From mead-baseline with Apache License 2.0 | 5 votes |
def check_re(b: bytes, regex: Pattern = None) -> bool: return True if regex.match(b) else False
Example #19
Source File: utils.py From ipmininet with GNU General Public License v2.0 | 5 votes |
def search_dns_reply(reply: str, regex: Pattern) \ -> Tuple[bool, Optional[Match]]: got_answer = False for line in reply.split("\n"): if got_answer: if "SECTION" in line: break # End of the answer section match = regex.match(line) if match is not None: return True, match # Got the right answer elif ";; ANSWER SECTION:" in line: # Beginning of the answer section got_answer = True return got_answer, None
Example #20
Source File: compareDB3.py From armi with Apache License 2.0 | 5 votes |
def _compareComponentData( out, refGroup, srcGroup, diffResults, exclusions: Optional[Sequence[Pattern]] = None ): exclusions = exclusions or [] compName = refGroup.name paramNames, nDiff = _compareH5Groups( out, refGroup, srcGroup, "{} parameters".format(compName) ) diffResults.addStructureDiffs(nDiff) for paramName in paramNames: fullName = "/".join((refGroup.name, paramName)) if any(pattern.match(fullName) for pattern in exclusions): runLog.debug( "Skipping comparison of {} since it is being ignored.".format(fullName) ) continue refDataset = refGroup[paramName] srcDataset = srcGroup[paramName] srcSpecial = srcDataset.attrs.get("specialFormatting", False) refSpecial = refDataset.attrs.get("specialFormatting", False) if srcSpecial ^ refSpecial: out.writeln( "Could not compare data because one uses special formatting, " "and the other does not. Ref: {} Src: {}".format(refSpecial, srcSpecial) ) diffResults.addDiff( refGroup.name, paramName, numpy.inf, numpy.inf, numpy.inf ) continue if srcSpecial or refSpecial: _diffSpecialData(refDataset, srcDataset, out, diffResults) else: _diffSimpleData(refDataset, srcDataset, out, diffResults)
Example #21
Source File: passgithelper.py From pass-git-helper with GNU Lesser General Public License v3.0 | 5 votes |
def _build_matcher(self, regex: str) -> Pattern: matcher = re.compile(regex) if matcher.groups != 1: raise ValueError( 'Provided regex "{regex}" must contain a single ' "capture group for the value to return.".format(regex=regex) ) return matcher
Example #22
Source File: __init__.py From nixpkgs-review with MIT License | 5 votes |
def regex_type(s: str) -> Pattern[str]: try: return re.compile(s) except re.error as e: raise argparse.ArgumentTypeError(f"'{s}' is not a valid regex: {e}")
Example #23
Source File: recwarn.py From pytest with MIT License | 5 votes |
def warns( expected_warning: Optional[Union["Type[Warning]", Tuple["Type[Warning]", ...]]], *, match: "Optional[Union[str, Pattern]]" = ... ) -> "WarningsChecker": raise NotImplementedError()
Example #24
Source File: recwarn.py From pytest with MIT License | 5 votes |
def deprecated_call( *, match: Optional[Union[str, "Pattern"]] = ... ) -> "WarningsRecorder": raise NotImplementedError()
Example #25
Source File: python_api.py From pytest with MIT License | 5 votes |
def __init__( self, expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], message: str, match_expr: Optional[Union[str, "Pattern"]] = None, ) -> None: self.expected_exception = expected_exception self.message = message self.match_expr = match_expr self.excinfo = None # type: Optional[_pytest._code.ExceptionInfo[_E]]
Example #26
Source File: python_api.py From pytest with MIT License | 5 votes |
def raises( expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]], *, match: "Optional[Union[str, Pattern]]" = ... ) -> "RaisesContext[_E]": ... # pragma: no cover
Example #27
Source File: code.py From pytest with MIT License | 5 votes |
def match(self, regexp: "Union[str, Pattern]") -> "Literal[True]": """ Check whether the regular expression `regexp` matches the string representation of the exception using :func:`python:re.search`. If it matches `True` is returned. If it doesn't match an `AssertionError` is raised. """ __tracebackhide__ = True assert re.search( regexp, str(self.value) ), "Pattern {!r} does not match {!r}".format(regexp, str(self.value)) # Return True to allow for "assert excinfo.match()". return True
Example #28
Source File: _filename.py From pathvalidate with MIT License | 5 votes |
def _get_sanitize_regexp(self) -> Pattern: if self.platform in [Platform.UNIVERSAL, Platform.WINDOWS]: return _RE_INVALID_WIN_FILENAME return _RE_INVALID_FILENAME
Example #29
Source File: configtypes.py From qutebrowser with GNU General Public License v3.0 | 5 votes |
def _compile_regex(self, pattern: str) -> typing.Pattern[str]: """Check if the given regex is valid. This is more complicated than it could be since there's a warning on invalid escapes with newer Python versions, and we want to catch that case and treat it as invalid. """ with warnings.catch_warnings(record=True) as recorded_warnings: warnings.simplefilter('always') try: compiled = re.compile(pattern, self.flags) except re.error as e: raise configexc.ValidationError( pattern, "must be a valid regex - " + str(e)) except RuntimeError: # pragma: no cover raise configexc.ValidationError( pattern, "must be a valid regex - recursion depth " "exceeded") assert recorded_warnings is not None for w in recorded_warnings: if (issubclass(w.category, DeprecationWarning) and str(w.message).startswith('bad escape')): raise configexc.ValidationError( pattern, "must be a valid regex - " + str(w.message)) warnings.warn(w.message) return compiled
Example #30
Source File: test_typing.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_union_str_pattern(self): # Shouldn't crash; see http://bugs.python.org/issue25390 A = Union[str, Pattern]