summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2020-10-25 09:49:58 -0500
committerptmcg <ptmcg@austin.rr.com>2020-10-25 09:49:58 -0500
commit7f68a2aa4386e8a075aabc92ca8b6582bcc25a42 (patch)
tree5de508e68765327c778e8a72cc781c60dee47076
parent5811c79597aa718df2e72fb95189233055a2ded3 (diff)
downloadpyparsing-git-7f68a2aa4386e8a075aabc92ca8b6582bcc25a42.tar.gz
minor perf changes
-rw-r--r--pyparsing/core.py11
-rw-r--r--pyparsing/results.py22
2 files changed, 14 insertions, 19 deletions
diff --git a/pyparsing/core.py b/pyparsing/core.py
index c05c93c..c242d40 100644
--- a/pyparsing/core.py
+++ b/pyparsing/core.py
@@ -2459,9 +2459,8 @@ class Regex(Token):
loc = result.end()
ret = ParseResults(result.group())
d = result.groupdict()
- if d:
- for k, v in d.items():
- ret[k] = v
+ for k, v in d.items():
+ ret[k] = v
return loc, ret
def parseImplAsGroupList(self, instring, loc, doActions=True):
@@ -2551,6 +2550,7 @@ class QuotedString(Token):
[['This is the "quote"']]
[['This is the quote with "embedded" quotes']]
"""
+ ws_map = ((r"\t", "\t"), (r"\n", "\n"), (r"\f", "\f"), (r"\r", "\r"))
def __init__(
self,
@@ -2661,8 +2661,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"}
- for wslit, wschar in ws_map.items():
+ for wslit, wschar in self.ws_map:
ret = ret.replace(wslit, wschar)
# replace escaped characters
@@ -3962,7 +3961,7 @@ class _MultipleMatch(ParseElementEnhance):
else:
preloc = loc
loc, tmptokens = self_expr_parse(instring, preloc, doActions)
- if tmptokens or tmptokens.haskeys():
+ if tmptokens:
tokens += tmptokens
except (ParseException, IndexError):
pass
diff --git a/pyparsing/results.py b/pyparsing/results.py
index a30e919..a375b07 100644
--- a/pyparsing/results.py
+++ b/pyparsing/results.py
@@ -69,6 +69,7 @@ class ParseResults:
- month: 12
- year: 1999
"""
+ null_values = (None, b"", "", [], ())
__slots__ = [
"_name",
@@ -141,10 +142,8 @@ class ParseResults:
if toklist is None:
toklist = []
- if isinstance(toklist, ParseResults.List):
- self._toklist = [toklist[:]]
- elif isinstance(toklist, (list, _generator_type)):
- self._toklist = list(toklist)
+ if isinstance(toklist, (list, _generator_type)):
+ self._toklist = [toklist[:]] if isinstance(toklist, ParseResults.List) else list(toklist)
else:
self._toklist = [toklist]
self._tokdict = dict()
@@ -156,16 +155,13 @@ class ParseResults:
self, toklist=None, name=None, asList=True, modal=True, isinstance=isinstance
):
self._modal = modal
- if name is not None and name:
- if not modal:
- self._all_names = {name}
+ if name not in (None, ""):
if isinstance(name, int):
name = str(name)
+ if not modal:
+ self._all_names = {name}
self._name = name
- if not (
- isinstance(toklist, (type(None), *str_type, list))
- and toklist in (None, "", [])
- ):
+ if toklist not in self.null_values:
if isinstance(toklist, str_type):
toklist = [toklist]
if asList:
@@ -238,7 +234,7 @@ class ParseResults:
return len(self._toklist)
def __bool__(self):
- return not not self._toklist
+ return not not self._toklist or not not self._tokdict
def __iter__(self):
return iter(self._toklist)
@@ -526,7 +522,7 @@ class ParseResults:
Returns a new copy of a :class:`ParseResults` object.
"""
ret = ParseResults(self._toklist)
- ret._tokdict = dict(self._tokdict.items())
+ ret._tokdict = dict(**self._tokdict)
ret._parent = self._parent
ret._all_names |= self._all_names
ret._name = self._name