diff options
author | ptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b> | 2016-01-23 07:51:12 +0000 |
---|---|---|
committer | ptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b> | 2016-01-23 07:51:12 +0000 |
commit | 58781833e130bd10c5854e12395cb00dab652962 (patch) | |
tree | 36ae196298046f00ebf62993673577a3a49eac2c | |
parent | 1999f06a582ba0e5b05a9b8c41eaba4b80e79b2b (diff) | |
download | pyparsing-58781833e130bd10c5854e12395cb00dab652962.tar.gz |
Fixed ParseResults.asDict() to correctly convert nested ParseResults values to dicts.
Fixed ParseResults.__radd__ to return other+self if not adding to 0.
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/trunk@315 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r-- | src/CHANGES | 3 | ||||
-rw-r--r-- | src/pyparsing.py | 13 |
2 files changed, 12 insertions, 4 deletions
diff --git a/src/CHANGES b/src/CHANGES index 1f3624b..bf7af4f 100644 --- a/src/CHANGES +++ b/src/CHANGES @@ -19,6 +19,9 @@ Version 2.0.8 - - Converted helper lambdas to functions to refactor and add docstring
support.
+- Fixed ParseResults.asDict() to correctly convert nested ParseResults
+ values to dicts.
+
Version 2.0.7 -
---------------------------
diff --git a/src/pyparsing.py b/src/pyparsing.py index 808e640..f1f19c8 100644 --- a/src/pyparsing.py +++ b/src/pyparsing.py @@ -58,7 +58,7 @@ The pyparsing module handles some of the problems that are typically vexing when """
__version__ = "2.0.8"
-__versionTime__ = "17 Jan 2016 21:22"
+__versionTime__ = "23 Jan 2016 01:33"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -504,7 +504,11 @@ class ParseResults(object): def __radd__(self, other):
if isinstance(other,int) and other == 0:
+ # useful for merging many ParseResults using sum() builtin
return self.copy()
+ else:
+ # this may raise a TypeError - so be it
+ return other + self
def __repr__( self ):
return "(%s, %s)" % ( repr( self.__toklist ), repr( self.__tokdict ) )
@@ -528,11 +532,12 @@ class ParseResults(object): return [res.asList() if isinstance(res,ParseResults) else res for res in self.__toklist]
def asDict( self ):
- """Returns the named parse results as dictionary."""
+ """Returns the named parse results as a nested dictionary."""
if PY_3:
- return dict( self.items() )
+ item_fn = self.items
else:
- return dict( self.iteritems() )
+ item_fn = self.iteritems
+ return dict((k,v.asDict()) if isinstance(v, ParseResults) else (k,v) for k,v in item_fn())
def copy( self ):
"""Returns a new copy of a C{ParseResults} object."""
|