From 3157a77c9584a69839db0f8274c87438b912e99b Mon Sep 17 00:00:00 2001 From: ptmcg Date: Wed, 30 Jan 2019 20:41:33 -0600 Subject: Update examples and unit tests to more preferred coding styles, imports for pyparsing_common as ppc and pyparsing_unicode as ppu --- examples/jsonParser.py | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'examples/jsonParser.py') 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 ) -- cgit v1.2.1