diff --git a/CHANGES b/CHANGES index eb6ab49f..ce5c3dfc 100644 --- a/CHANGES +++ b/CHANGES @@ -18,6 +18,8 @@ Version 3.1.2 - in development - Updated pep8 synonym wrappers for better type checking compatibility. PR submitted by Ricardo Coccioli. +- Some code refactoring to reduce code nesting, PRs submitted by InSync. + Version 3.1.1 - July, 2023 -------------------------- diff --git a/pyparsing/core.py b/pyparsing/core.py index 9f1e50c2..041ff6e6 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -1289,7 +1289,7 @@ def scan_string( except ParseBaseException as exc: if ParserElement.verbose_stacktrace: raise - + # catch and re-raise exception from here, clears out pyparsing internal stack trace raise exc.with_traceback(None) @@ -2316,7 +2316,7 @@ def must_skip(t): def show_skip(t): if t._skipped.as_list()[-1:] == [""]: t.pop("_skipped") - t["_skipped"] = "missing <" + repr(self.anchor) + ">" + t["_skipped"] = f"missing <{self.anchor!r}>" return ( self.anchor + skipper().add_parse_action(must_skip) @@ -2927,8 +2927,8 @@ def parseImpl(self, instring, loc, doActions=True): elif self.maxSpecified and loc < instrlen and instring[loc] in bodychars: throwException = True elif self.asKeyword and ( - start > 0 and instring[start - 1] in bodychars - or loc < instrlen and instring[loc] in bodychars + (start > 0 and instring[start - 1] in bodychars) + or (loc < instrlen and instring[loc] in bodychars) ): throwException = True @@ -3782,7 +3782,7 @@ def ignore(self, other) -> ParserElement: return self def _generateDefaultName(self) -> str: - return f"{self.__class__.__name__}:({str(self.exprs)})" + return f"{self.__class__.__name__}:({self.exprs})" def streamline(self) -> ParserElement: if self.streamlined: @@ -4032,7 +4032,7 @@ def _generateDefaultName(self) -> str: # strip off redundant inner {}'s while len(inner) > 1 and inner[0 :: len(inner) - 1] == "{}": inner = inner[1:-1] - return "{" + inner + "}" + return f"{{{inner}}}" class Or(ParseExpression): @@ -4154,9 +4154,7 @@ def parseImpl(self, instring, loc, doActions=True): maxException.msg = self.errmsg raise maxException - raise ParseException( - instring, loc, "no defined alternatives to match", self - ) + raise ParseException(instring, loc, "no defined alternatives to match", self) def __ixor__(self, other): if isinstance(other, str_type): @@ -4166,7 +4164,7 @@ def __ixor__(self, other): return self.append(other) # Or([self, other]) def _generateDefaultName(self) -> str: - return "{" + " ^ ".join(str(e) for e in self.exprs) + "}" + return f"{{{' ^ '.join(str(e) for e in self.exprs)}}}" def _setResultsName(self, name, listAllMatches=False): if ( @@ -4263,9 +4261,7 @@ def parseImpl(self, instring, loc, doActions=True): maxException.msg = self.errmsg raise maxException - raise ParseException( - instring, loc, "no defined alternatives to match", self - ) + raise ParseException(instring, loc, "no defined alternatives to match", self) def __ior__(self, other): if isinstance(other, str_type): @@ -4275,7 +4271,7 @@ def __ior__(self, other): return self.append(other) # MatchFirst([self, other]) def _generateDefaultName(self) -> str: - return "{" + " | ".join(str(e) for e in self.exprs) + "}" + return f"{{{' | '.join(str(e) for e in self.exprs)}}}" def _setResultsName(self, name, listAllMatches=False): if ( @@ -4472,7 +4468,7 @@ def parseImpl(self, instring, loc, doActions=True): return loc, total_results def _generateDefaultName(self) -> str: - return "{" + " & ".join(str(e) for e in self.exprs) + "}" + return f"{{{' & '.join(str(e) for e in self.exprs)}}}" class ParseElementEnhance(ParserElement): @@ -4570,7 +4566,7 @@ def validate(self, validateTrace=None) -> None: self._checkRecursion([]) def _generateDefaultName(self) -> str: - return f"{self.__class__.__name__}:({str(self.expr)})" + return f"{self.__class__.__name__}:({self.expr})" # Compatibility synonyms # fmt: off @@ -4805,9 +4801,7 @@ def parseImpl(self, instring, loc=0, doActions=True): for offset in range(1, min(loc, self.retreat + 1) + 1): try: # print('trying', offset, instring_slice, repr(instring_slice[loc - offset:])) - _, ret = test_expr._parse( - instring_slice, len(instring_slice) - offset - ) + _, ret = test_expr._parse(instring_slice, len(instring_slice) - offset) except ParseBaseException as pbe: last_expr = pbe else: @@ -4900,7 +4894,7 @@ def parseImpl(self, instring, loc, doActions=True): return loc, [] def _generateDefaultName(self) -> str: - return "~{" + str(self.expr) + "}" + return f"~{{{self.expr}}}" class _MultipleMatch(ParseElementEnhance): @@ -5005,7 +4999,7 @@ class OneOrMore(_MultipleMatch): """ def _generateDefaultName(self) -> str: - return "{" + str(self.expr) + "}..." + return f"{{{self.expr}}}..." class ZeroOrMore(_MultipleMatch): @@ -5039,7 +5033,7 @@ def parseImpl(self, instring, loc, doActions=True): return loc, ParseResults([], name=self.resultsName) def _generateDefaultName(self) -> str: - return "[" + str(self.expr) + "]..." + return f"[{self.expr}]..." class DelimitedList(ParseElementEnhance): @@ -5103,7 +5097,8 @@ def __init__( super().__init__(delim_list_expr, savelist=True) def _generateDefaultName(self) -> str: - return "{0} [{1} {0}]...".format(self.content.streamline(), self.raw_delim) + content_expr = self.content.streamline() + return f"{content_expr} [{self.raw_delim} {content_expr}]..." class _NullToken: @@ -5185,7 +5180,7 @@ def _generateDefaultName(self) -> str: # strip off redundant inner {}'s while len(inner) > 1 and inner[0 :: len(inner) - 1] == "{}": inner = inner[1:-1] - return "[" + inner + "]" + return f"[{inner}]" Optional = Opt @@ -5570,7 +5565,7 @@ def _generateDefaultName(self) -> str: else: retString = "None" finally: - return self.__class__.__name__ + ": " + retString + return f"{self.__class__.__name__}: {retString}" def copy(self) -> ParserElement: if self.expr is not None: @@ -5881,7 +5876,7 @@ def z(*paArgs): thisFunc = f.__name__ s, l, t = paArgs[-3:] if len(paArgs) > 3: - thisFunc = paArgs[0].__class__.__name__ + "." + thisFunc + thisFunc = f"{paArgs[0].__class__.__name__}.{thisFunc}" sys.stderr.write(f">>entering {thisFunc}(line: {line(l, s)!r}, {l}, {t!r})\n") try: ret = f(*paArgs) diff --git a/pyparsing/exceptions.py b/pyparsing/exceptions.py index 11ba941f..5d21223a 100644 --- a/pyparsing/exceptions.py +++ b/pyparsing/exceptions.py @@ -83,7 +83,7 @@ def explain_exception(exc, depth=16): ret = [] if isinstance(exc, ParseBaseException): ret.append(exc.line) - ret.append(" " * (exc.column - 1) + "^") + ret.append(f"{' ' * (exc.column - 1)}^") ret.append(f"{type(exc).__name__}: {exc}") if depth <= 0: @@ -96,18 +96,14 @@ def explain_exception(exc, depth=16): f_self = frm.f_locals.get("self", None) if isinstance(f_self, ParserElement): - if not frm.f_code.co_name.startswith( - ("parseImpl", "_parseNoCache") - ): + if not frm.f_code.co_name.startswith(("parseImpl", "_parseNoCache")): continue if id(f_self) in seen: continue seen.add(id(f_self)) self_type = type(f_self) - ret.append( - f"{self_type.__module__}.{self_type.__name__} - {f_self}" - ) + ret.append(f"{self_type.__module__}.{self_type.__name__} - {f_self}") elif f_self is not None: self_type = type(f_self) diff --git a/pyparsing/helpers.py b/pyparsing/helpers.py index 27d0d577..9a12f8dd 100644 --- a/pyparsing/helpers.py +++ b/pyparsing/helpers.py @@ -74,7 +74,7 @@ def count_field_parse_action(s, l, t): intExpr = intExpr.copy() intExpr.set_name("arrayLen") intExpr.add_parse_action(count_field_parse_action, call_during_try=True) - return (intExpr + array_expr).set_name("(len) " + str(expr) + "...") + return (intExpr + array_expr).set_name(f"(len) {expr}...") def match_previous_literal(expr: ParserElement) -> ParserElement: diff --git a/pyparsing/results.py b/pyparsing/results.py index ab05f33c..31b33102 100644 --- a/pyparsing/results.py +++ b/pyparsing/results.py @@ -193,13 +193,9 @@ def __init__( if asList: if isinstance(toklist, ParseResults): - self[name] = _ParseResultsWithOffset( - ParseResults(toklist._toklist), 0 - ) + self[name] = _ParseResultsWithOffset(ParseResults(toklist._toklist), 0) else: - self[name] = _ParseResultsWithOffset( - ParseResults(toklist[0]), 0 - ) + self[name] = _ParseResultsWithOffset(ParseResults(toklist[0]), 0) self[name]._name = name return diff --git a/pyparsing/testing.py b/pyparsing/testing.py index ab71808c..014fe730 100644 --- a/pyparsing/testing.py +++ b/pyparsing/testing.py @@ -194,9 +194,7 @@ def assertRunTestResults( # expected should be a tuple containing a list and/or a dict or an exception, # and optional failure message string # an empty tuple will skip any result validation - fail_msg = next( - (exp for exp in expected if isinstance(exp, str)), None - ) + fail_msg = next((exp for exp in expected if isinstance(exp, str)), None) expected_exception = next( ( exp diff --git a/pyparsing/unicode.py b/pyparsing/unicode.py index 1ba4f858..426b8b23 100644 --- a/pyparsing/unicode.py +++ b/pyparsing/unicode.py @@ -103,8 +103,7 @@ def identbodychars(cls): plus the digits 0-9, and · (Unicode MIDDLE DOT) """ identifier_chars = set( - c for c in cls._chars_for_ranges - if ("_" + c).isidentifier() + c for c in cls._chars_for_ranges if ("_" + c).isidentifier() ) return "".join(sorted(identifier_chars | set(cls.identchars + "0123456789·")))