diff options
author | Paul McGuire <ptmcg@austin.rr.com> | 2019-07-11 06:32:56 -0500 |
---|---|---|
committer | Paul McGuire <ptmcg@austin.rr.com> | 2019-07-11 06:32:56 -0500 |
commit | ddaf822e2da67f850aebe61e43d616b731a63e48 (patch) | |
tree | d5393c7da4766485bb2ce24812839fb753a4a0d2 | |
parent | 6f2441a1ea71869cf049d2edb5f9f685fc0d92a5 (diff) | |
download | pyparsing-git-ddaf822e2da67f850aebe61e43d616b731a63e48.tar.gz |
Simplify from_dict signature, support nested dict -> nested ParseResults
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | pyparsing.py | 34 |
2 files changed, 22 insertions, 17 deletions
@@ -125,9 +125,8 @@ Version 2.4.1 - July, 2019 MatchFirst or Or expressions containing an And expression. - Added ParseResults.from_dict classmethod, to simplify creation - of a ParseResults with results names. May be called with a dict - argument, or with a series of named arguments (or both). This - makes it easy to add a sub-level of named items to the parsed + of a ParseResults with results names using a dict, which may be nested. + This makes it easy to add a sub-level of named items to the parsed tokens in a parse action. - Added asKeyword argument (default=False) to oneOf, to force diff --git a/pyparsing.py b/pyparsing.py index 06d9aba..f97d8c2 100644 --- a/pyparsing.py +++ b/pyparsing.py @@ -96,7 +96,7 @@ classes inherit from. Use the docstrings for examples of how to: """ __version__ = "2.4.1" -__versionTime__ = "11 Jul 2019 03:14 UTC" +__versionTime__ = "11 Jul 2019 11:31 UTC" __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>" import string @@ -129,11 +129,11 @@ except ImportError: try: # Python 3 from collections.abc import Iterable - from collections.abc import MutableMapping + from collections.abc import MutableMapping, Mapping except ImportError: # Python 2.7 from collections import Iterable - from collections import MutableMapping + from collections import MutableMapping, Mapping try: from collections import OrderedDict as _OrderedDict @@ -1174,26 +1174,32 @@ class ParseResults(object): return (dir(type(self)) + list(self.keys())) @classmethod - def from_dict(cls, other=None, **kwargs): + def from_dict(cls, other, name=None): """ - Helper classmethod to construct a ParseResults from either a dict or a sequence of named arguments, - preserving the name-value relations as results names. + Helper classmethod to construct a ParseResults from a dict, preserving the + name-value relations as results names. If an optional 'name' argument is + given, a nested ParseResults will be returned """ - if other is not None: - items = itertools.chain(other.items(), kwargs.items()) - else: - items = kwargs.items() - def is_iterable(obj): - try: iter(obj) - except Exception: return False + try: + iter(obj) + except Exception: + return False else: if PY_3: return not isinstance(obj, (str, bytes)) else: return not isinstance(obj, basestring) - return sum(cls([v], name=k, asList=is_iterable(v)) for k, v in items) + ret = cls([]) + for k, v in other.items(): + if isinstance(v, Mapping): + ret += cls.from_dict(v, name=k) + else: + ret += cls([v], name=k, asList=is_iterable(v)) + if name is not None: + ret = cls([ret], name=name) + return ret MutableMapping.register(ParseResults) |