Skip to content

Commit

Permalink
Add CHANGES note for source refactoring, plus a few more f-strings an…
Browse files Browse the repository at this point in the history
…d blackening
  • Loading branch information
ptmcg committed Nov 20, 2023
1 parent 0b6ec8e commit 2a1a8e8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 45 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -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
--------------------------
Expand Down
47 changes: 21 additions & 26 deletions pyparsing/core.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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 (
Expand Down Expand Up @@ -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):
Expand All @@ -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 (
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -5005,7 +4999,7 @@ class OneOrMore(_MultipleMatch):
"""

def _generateDefaultName(self) -> str:
return "{" + str(self.expr) + "}..."
return f"{{{self.expr}}}..."


class ZeroOrMore(_MultipleMatch):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 3 additions & 7 deletions pyparsing/exceptions.py
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyparsing/helpers.py
Expand Up @@ -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:
Expand Down
8 changes: 2 additions & 6 deletions pyparsing/results.py
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions pyparsing/testing.py
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pyparsing/unicode.py
Expand Up @@ -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·")))

Expand Down

0 comments on commit 2a1a8e8

Please sign in to comment.