diff options
author | ptmcg <ptmcg@austin.rr.com> | 2019-01-30 20:41:33 -0600 |
---|---|---|
committer | ptmcg <ptmcg@austin.rr.com> | 2019-01-30 20:41:33 -0600 |
commit | 3157a77c9584a69839db0f8274c87438b912e99b (patch) | |
tree | 5e93a6e21715f9afcbc55eba56f4a5f962b67a46 /examples/jsonParser.py | |
parent | f10a02039a6039f8a42b0cc99bea51623676cf01 (diff) | |
download | pyparsing-git-3157a77c9584a69839db0f8274c87438b912e99b.tar.gz |
Update examples and unit tests to more preferred coding styles, imports for pyparsing_common as ppc and pyparsing_unicode as ppu
Diffstat (limited to 'examples/jsonParser.py')
-rw-r--r-- | examples/jsonParser.py | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/examples/jsonParser.py b/examples/jsonParser.py index 6319c36..fbf76b4 100644 --- a/examples/jsonParser.py +++ b/examples/jsonParser.py @@ -33,29 +33,30 @@ value null
"""
-from pyparsing import *
+import pyparsing as pp
+from pyparsing import pyparsing_common as ppc
def make_keyword(kwd_str, kwd_value):
- return Keyword(kwd_str).setParseAction(replaceWith(kwd_value))
+ return pp.Keyword(kwd_str).setParseAction(pp.replaceWith(kwd_value))
TRUE = make_keyword("true", True)
FALSE = make_keyword("false", False)
NULL = make_keyword("null", None)
-LBRACK, RBRACK, LBRACE, RBRACE, COLON = map(Suppress, "[]{}:")
+LBRACK, RBRACK, LBRACE, RBRACE, COLON = map(pp.Suppress, "[]{}:")
-jsonString = dblQuotedString().setParseAction(removeQuotes)
-jsonNumber = pyparsing_common.number()
+jsonString = pp.dblQuotedString().setParseAction(pp.removeQuotes)
+jsonNumber = ppc.number()
-jsonObject = Forward()
-jsonValue = Forward()
-jsonElements = delimitedList( jsonValue )
-jsonArray = Group(LBRACK + Optional(jsonElements, []) + RBRACK)
-jsonValue << (jsonString | jsonNumber | Group(jsonObject) | jsonArray | TRUE | FALSE | NULL)
-memberDef = Group(jsonString + COLON + jsonValue)
-jsonMembers = delimitedList(memberDef)
-jsonObject << Dict(LBRACE + Optional(jsonMembers) + RBRACE)
+jsonObject = pp.Forward()
+jsonValue = pp.Forward()
+jsonElements = pp.delimitedList( jsonValue )
+jsonArray = pp.Group(LBRACK + pp.Optional(jsonElements, []) + RBRACK)
+jsonValue << (jsonString | jsonNumber | pp.Group(jsonObject) | jsonArray | TRUE | FALSE | NULL)
+memberDef = pp.Group(jsonString + COLON + jsonValue)
+jsonMembers = pp.delimitedList(memberDef)
+jsonObject << pp.Dict(LBRACE + pp.Optional(jsonMembers) + RBRACE)
-jsonComment = cppStyleComment
+jsonComment = pp.cppStyleComment
jsonObject.ignore(jsonComment)
@@ -90,12 +91,11 @@ if __name__ == "__main__": }
"""
- import pprint
results = jsonObject.parseString(testdata)
- pprint.pprint( results.asList() )
+ results.pprint()
print()
def testPrint(x):
- print(type(x),repr(x))
+ print(type(x), repr(x))
print(list(results.glossary.GlossDiv.GlossList.keys()))
testPrint( results.glossary.title )
testPrint( results.glossary.GlossDiv.GlossList.ID )
|