diff options
author | Paul McGuire <ptmcg@users.noreply.github.com> | 2020-07-19 01:27:26 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@users.noreply.github.com> | 2020-07-19 01:27:26 -0500 |
commit | 11cdffe3dff0449d7f49c90aaefb572c01ddb580 (patch) | |
tree | 21223cfbd9868020f4a43b26d19b880f6163378e | |
parent | 31679fac4fb3811004e5dc09c1465d7117edc830 (diff) | |
download | pyparsing-git-11cdffe3dff0449d7f49c90aaefb572c01ddb580.tar.gz |
Replace last-century '%' string interp with .format() usage
-rw-r--r-- | pyparsing/actions.py | 2 | ||||
-rw-r--r-- | pyparsing/core.py | 130 | ||||
-rw-r--r-- | pyparsing/exceptions.py | 10 | ||||
-rw-r--r-- | pyparsing/results.py | 13 |
4 files changed, 85 insertions, 70 deletions
diff --git a/pyparsing/actions.py b/pyparsing/actions.py index 921b23f..0c185ab 100644 --- a/pyparsing/actions.py +++ b/pyparsing/actions.py @@ -11,7 +11,7 @@ def matchOnlyAtCol(n): def verifyCol(strg, locn, toks): if col(locn, strg) != n: - raise ParseException(strg, locn, "matched token not at column %d" % n) + raise ParseException(strg, locn, "matched token not at column {}".format(n)) return verifyCol diff --git a/pyparsing/core.py b/pyparsing/core.py index a63f6e1..2c08bd9 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -226,11 +226,9 @@ def conditionAsParseAction(fn, message=None, fatal=False): def _defaultStartDebugAction(instring, loc, expr): print( ( - "Match " - + str(expr) - + " at loc " - + str(loc) - + "(%d,%d)" % (lineno(loc, instring), col(loc, instring)) + "Match {} at loc {}({},{})".format( + expr, loc, lineno(loc, instring), col(loc, instring) + ) ) ) @@ -306,17 +304,22 @@ class ParserElement(ABC): self.skipWhitespace = True self.whiteChars = set(ParserElement.DEFAULT_WHITE_CHARS) self.copyDefaultWhiteChars = True - self.mayReturnEmpty = False # used when checking for left-recursion + # used when checking for left-recursion + self.mayReturnEmpty = False self.keepTabs = False self.ignoreExprs = list() self.debug = False self.streamlined = False - self.mayIndexError = True # used to optimize exception handling for subclasses that don't advance parse index + # optimize exception handling for subclasses that don't advance parse index + self.mayIndexError = True self.errmsg = "" - self.modalResults = True # used to mark results names as modal (report only last) or cumulative (list all) - self.debugActions = (None, None, None) # custom debug actions + # mark results names as modal (report only last) or cumulative (list all) + self.modalResults = True + # custom debug actions + self.debugActions = (None, None, None) self.re = None - self.callPreparse = True # used to avoid redundant calls to preParse + # avoid redundant calls to preParse + self.callPreparse = True self.callDuringTry = False def copy(self): @@ -443,9 +446,7 @@ class ParserElement(ABC): # note that integer fields are now ints, not strings date_str.parseString("1999/12/31") # -> [1999, '/', 12, '/', 31] """ - if list(fns) == [ - None, - ]: + if list(fns) == [None]: self.parseAction = [] else: if not all(callable(fn) for fn in fns): @@ -549,7 +550,7 @@ class ParserElement(ABC): debugging = self.debug # and doActions) if debugging or self.failAction: - # print("Match", self, "at loc", loc, "(%d, %d)" % (lineno(loc, instring), col(loc, instring))) + # print("Match {} at loc {}({}, {})".format(self, loc, lineno(loc, instring), col(loc, instring))) if self.debugActions[TRY]: self.debugActions[TRY](instring, loc, self) try: @@ -1028,7 +1029,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1043,7 +1046,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1058,7 +1063,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1109,13 +1116,15 @@ class ParserElement(ABC): optElements -= minElements else: raise TypeError( - "cannot multiply 'ParserElement' and ('%s', '%s') objects", - type(other[0]), - type(other[1]), + "cannot multiply ParserElement and ({}) objects".format( + ",".join(type(item).__name__ for item in other) + ) ) else: raise TypeError( - "cannot multiply 'ParserElement' and '%s' objects", type(other) + "cannot multiply ParserElement and {} objects".format( + type(other).__name__ + ) ) if minElements < 0: @@ -1163,7 +1172,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1178,7 +1189,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1193,7 +1206,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1208,7 +1223,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1223,7 +1240,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1238,7 +1257,9 @@ class ParserElement(ABC): other = self._literalStringClass(other) if not isinstance(other, ParserElement): warnings.warn( - "Cannot combine element of type %s with ParserElement" % type(other), + "Cannot combine element of type {} with ParserElement".format( + type(other).__name__ + ), SyntaxWarning, stacklevel=2, ) @@ -1254,7 +1275,7 @@ class ParserElement(ABC): def __iter__(self): # must implement __iter__ to override legacy use of sequential access to __getitem__ to # iterate over a sequence - raise TypeError("%r object is not iterable" % self.__class__.__name__) + raise TypeError("{} object is not iterable".format(self.__class__.__name__)) def __getitem__(self, key): """ @@ -2060,9 +2081,8 @@ class CloseMatch(Token): super().__init__() self.match_string = match_string self.maxMismatches = maxMismatches - self.errmsg = "Expected %r (with up to %d mismatches)" % ( - self.match_string, - self.maxMismatches, + self.errmsg = "Expected {!r} (with up to {} mismatches)".format( + self.match_string, self.maxMismatches ) self.mayIndexError = False self.mayReturnEmpty = False @@ -2219,14 +2239,16 @@ class Word(Token): min == 1 and max == 0 and exact == 0 ): if self.bodyCharsOrig == self.initCharsOrig: - self.reString = "[%s]+" % _collapseStringToRanges(self.initCharsOrig) + self.reString = "[{}]+".format( + _collapseStringToRanges(self.initCharsOrig) + ) elif len(self.initCharsOrig) == 1: - self.reString = "%s[%s]*" % ( + self.reString = "{}[{}]*".format( re.escape(self.initCharsOrig), _collapseStringToRanges(self.bodyCharsOrig), ) else: - self.reString = "[%s][%s]*" % ( + self.reString = "[{}][{}]*".format( _collapseStringToRanges(self.initCharsOrig), _collapseStringToRanges(self.bodyCharsOrig), ) @@ -2252,7 +2274,7 @@ class Word(Token): if self.initCharsOrig != self.bodyCharsOrig: return "W:({}, {})".format( - charsAsStr(self.initCharsOrig), charsAsStr(self.bodyCharsOrig), + charsAsStr(self.initCharsOrig), charsAsStr(self.bodyCharsOrig) ) else: return "W:({})".format(charsAsStr(self.initCharsOrig)) @@ -2366,7 +2388,7 @@ class Regex(Token): self.reString = self.pattern except sre_constants.error: warnings.warn( - "invalid pattern (%s) passed to Regex" % pattern, + "invalid pattern ({!r}) passed to Regex".format(pattern), SyntaxWarning, stacklevel=2, ) @@ -2552,14 +2574,14 @@ class QuotedString(Token): if multiline: self.flags = re.MULTILINE | re.DOTALL - self.pattern = r"%s(?:[^%s%s]" % ( + self.pattern = r"{}(?:[^{}{}]".format( re.escape(self.quoteChar), _escapeRegexRangeChars(self.endQuoteChar[0]), (escChar is not None and _escapeRegexRangeChars(escChar) or ""), ) else: self.flags = 0 - self.pattern = r"%s(?:[^%s\n\r%s]" % ( + self.pattern = r"{}(?:[^{}\n\r{}]".format( re.escape(self.quoteChar), _escapeRegexRangeChars(self.endQuoteChar[0]), (escChar is not None and _escapeRegexRangeChars(escChar) or ""), @@ -2568,8 +2590,7 @@ class QuotedString(Token): self.pattern += ( "|(?:" + ")|(?:".join( - "%s[^%s]" - % ( + "{}[^{}]".format( re.escape(self.endQuoteChar[:i]), _escapeRegexRangeChars(self.endQuoteChar[i]), ) @@ -2579,11 +2600,11 @@ class QuotedString(Token): ) if escQuote: - self.pattern += r"|(?:%s)" % re.escape(escQuote) + self.pattern += r"|(?:{})".format(re.escape(escQuote)) if escChar: - self.pattern += r"|(?:%s.)" % re.escape(escChar) + self.pattern += r"|(?:{}.)".format(re.escape(escChar)) self.escCharReplacePattern = re.escape(self.escChar) + "(.)" - self.pattern += r")*%s" % re.escape(self.endQuoteChar) + self.pattern += r")*{}".format(re.escape(self.endQuoteChar)) try: self.re = re.compile(self.pattern, self.flags) @@ -2591,7 +2612,7 @@ class QuotedString(Token): self.re_match = self.re.match except sre_constants.error: warnings.warn( - "invalid pattern (%s) passed to Regex" % self.pattern, + "invalid pattern {!r} passed to Regex".format(self.pattern), SyntaxWarning, stacklevel=2, ) @@ -2603,7 +2624,7 @@ class QuotedString(Token): def _generateDefaultName(self): return "quoted string, starting with {} ending with {}".format( - self.quoteChar, self.endQuoteChar, + self.quoteChar, self.endQuoteChar ) def parseImpl(self, instring, loc, doActions=True): @@ -2626,12 +2647,7 @@ class QuotedString(Token): if isinstance(ret, str_type): # replace escaped whitespace if "\\" in ret and self.convertWhitespaceEscapes: - ws_map = { - r"\t": "\t", - r"\n": "\n", - r"\f": "\f", - r"\r": "\r", - } + ws_map = {r"\t": "\t", r"\n": "\n", r"\f": "\f", r"\r": "\r"} for wslit, wschar in ws_map.items(): ret = ret.replace(wslit, wschar) @@ -3610,7 +3626,9 @@ class Each(ParseExpression): if tmpReqd: missing = ", ".join(str(e) for e in tmpReqd) raise ParseException( - instring, loc, "Missing one or more required elements (%s)" % missing + instring, + loc, + "Missing one or more required elements ({})".format(missing), ) # add any unmatched Optionals, in case they have default values defined @@ -3717,7 +3735,7 @@ class ParseElementEnhance(ParserElement): self._checkRecursion([]) def _generateDefaultName(self): - return "%s:(%s)" % (self.__class__.__name__, str(self.expr)) + return "{}:({})".format(self.__class__.__name__, str(self.expr)) class FollowedBy(ParseElementEnhance): @@ -4589,14 +4607,14 @@ def traceParseAction(f): if len(paArgs) > 3: thisFunc = paArgs[0].__class__.__name__ + "." + thisFunc sys.stderr.write( - ">>entering %s(line: '%s', %d, %r)\n" % (thisFunc, line(l, s), l, t) + ">>entering {}(line: {!r}, {}, {!r})\n".format(thisFunc, line(l, s), l, t) ) try: ret = f(*paArgs) except Exception as exc: - sys.stderr.write("<<leaving %s (exception: %s)\n" % (thisFunc, exc)) + sys.stderr.write("<<leaving {} (exception: {})\n".format(thisFunc, exc)) raise - sys.stderr.write("<<leaving %s (ret: %r)\n" % (thisFunc, ret)) + sys.stderr.write("<<leaving {} (ret: {!r})\n".format(thisFunc, ret)) return ret try: diff --git a/pyparsing/exceptions.py b/pyparsing/exceptions.py index 978e3d1..3a92fdf 100644 --- a/pyparsing/exceptions.py +++ b/pyparsing/exceptions.py @@ -119,12 +119,8 @@ class ParseBaseException(Exception): ) else: foundstr = "" - return "%s%s (at char %d), (line:%d, col:%d)" % ( - self.msg, - foundstr, - self.loc, - self.lineno, - self.column, + return "{}{} (at char {}), (line:{}, col:{})".format( + self.msg, foundstr, self.loc, self.lineno, self.column ) def __repr__(self): @@ -245,4 +241,4 @@ class RecursiveGrammarException(Exception): self.parseElementTrace = parseElementList def __str__(self): - return "RecursiveGrammarException: %s" % self.parseElementTrace + return "RecursiveGrammarException: {}".format(self.parseElementTrace) diff --git a/pyparsing/results.py b/pyparsing/results.py index c3fcd2d..cac1a3c 100644 --- a/pyparsing/results.py +++ b/pyparsing/results.py @@ -47,7 +47,7 @@ class ParseResults: result = date_str.parseString("1999/12/31") def test(s, fn=repr): - print("%s -> %s" % (s, fn(eval(s)))) + print("{} -> {}".format(s, fn(eval(s)))) test("list(result)") test("result[0]") test("result['month']") @@ -254,7 +254,9 @@ class ParseResults: if k == "default": args = (args[0], v) else: - raise TypeError("pop() got an unexpected keyword argument '%s'" % k) + raise TypeError( + "pop() got an unexpected keyword argument {!r}".format(k) + ) if isinstance(args[0], int) or len(args) == 1 or args[0] in self: index = args[0] ret = self[index] @@ -395,7 +397,7 @@ class ParseResults: return other + self def __repr__(self): - return "(%s, %s)" % (repr(self._toklist), self.asDict()) + return "({!r}, {})".format(self._toklist, self.asDict()) def __str__(self): return ( @@ -560,7 +562,7 @@ class ParseResults: for k, v in items: if out: out.append(NL) - out.append("%s%s- %s: " % (indent, (" " * _depth), k)) + out.append("{}{}- {}: ".format(indent, (" " * _depth), k)) if isinstance(v, ParseResults): if v: out.append( @@ -580,8 +582,7 @@ class ParseResults: for i, vv in enumerate(v): if isinstance(vv, ParseResults): out.append( - "\n%s%s[%d]:\n%s%s%s" - % ( + "\n{}{}[{}]:\n{}{}{}".format( indent, (" " * (_depth)), i, |