Thread: [Pyparsing] Parsing object structure
Brought to you by:
ptmcg
From: Claus R. <cla...@ro...> - 2012-09-18 08:30:59
|
Hi, i try to parse an object structure which is using open and close tags and containing lists of another objects. Example: NetSet{ name={LAN SIDE} oid={123998333,723663,2625521122} readOnly={0} origin={} global={0} comment={Voice Server UC} list={ NetEntry{ name={} readOnly={0} origin={} global={0} comment={} addr={192.169.0.0/24} } } neglist={ } } I tried to start with following code structure and don't know if it makes sense or not. from pyparsing import * LBRACE,RBRACE,SEMI,EQ,PCT = map(Suppress,"{};=%") comment = SEMI + restOfLine keyName = Word(alphas) text = Word( alphanums, "-", ",") text = Word(printables) num = Word(nums) ip = Combine(Word(nums) + ('.' + Word(nums))*3) v_text = keyName + EQ + LBRACE + Optional(text) + RBRACE v_num = keyName + EQ + LBRACE + Optional(num) + RBRACE v_ip = keyName + EQ + LBRACE + Optional(ip) + RBRACE NETENTRY = ( "NetEntry" + LBRACE + ZeroOrMore(Group(v_text | v_num | v_ip)) + RBRACE ) NETSET_PLIST = ( "list" + EQ + LBRACE + ZeroOrMore(NETENTRY) + RBRACE ) NETSET_NLIST = "neglist" + EQ + LBRACE + ZeroOrMore(Group(NETENTRY)) + RBRACE NETSET = "NetSet" + LBRACE + ZeroOrMore(Group(v_text | v_num | NETSET_PLIST | NETSET_NLIST)) + RBRACE rule_ref = NETSET for mr in rule_ref.parseFile("sample"): print mr I get following result: pyparsing.ParseException: Expected "}" (at char 48), (line:2, col:25) It seems NETSET_PLIST does not work, perhaps my code is the wrong approach. Thanks a lot Claus |
From: Paul M. <pt...@au...> - 2012-09-18 12:22:58
|
In your definition of 'text', you only match a single word - "name={LAN SIDE}" contains a text value with whitespace, more than one word. Also, beware of making 'text' match too much, you may end up including your trailing '}' as part of the text (as you will with your current definition of text as "Word(printables)"). -- Paul -----Original Message----- From: Claus Rosenberger [mailto:cla...@ro...] Sent: Tuesday, September 18, 2012 3:13 AM To: pyp...@li... Subject: [Pyparsing] Parsing object structure Hi, i try to parse an object structure which is using open and close tags and containing lists of another objects. Example: NetSet{ name={LAN SIDE} oid={123998333,723663,2625521122} readOnly={0} origin={} global={0} comment={Voice Server UC} list={ NetEntry{ name={} readOnly={0} origin={} global={0} comment={} addr={192.169.0.0/24} } } neglist={ } } I tried to start with following code structure and don't know if it makes sense or not. from pyparsing import * LBRACE,RBRACE,SEMI,EQ,PCT = map(Suppress,"{};=%") comment = SEMI + restOfLine keyName = Word(alphas) text = Word( alphanums, "-", ",") text = Word(printables) num = Word(nums) ip = Combine(Word(nums) + ('.' + Word(nums))*3) v_text = keyName + EQ + LBRACE + Optional(text) + RBRACE v_num = keyName + EQ + LBRACE + Optional(num) + RBRACE v_ip = keyName + EQ + LBRACE + Optional(ip) + RBRACE NETENTRY = ( "NetEntry" + LBRACE + ZeroOrMore(Group(v_text | v_num | v_ip)) + RBRACE ) NETSET_PLIST = ( "list" + EQ + LBRACE + ZeroOrMore(NETENTRY) + RBRACE ) NETSET_NLIST = "neglist" + EQ + LBRACE + ZeroOrMore(Group(NETENTRY)) + RBRACE NETSET = "NetSet" + LBRACE + ZeroOrMore(Group(v_text | v_num | NETSET_PLIST | NETSET_NLIST)) + RBRACE rule_ref = NETSET for mr in rule_ref.parseFile("sample"): print mr I get following result: pyparsing.ParseException: Expected "}" (at char 48), (line:2, col:25) It seems NETSET_PLIST does not work, perhaps my code is the wrong approach. Thanks a lot Claus ---------------------------------------------------------------------------- -- Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users |
From: Claus R. <cla...@ro...> - 2012-09-20 10:30:50
|
Ok, i changed it to the minimal set i need at the moment. text = Word( alphanums + "," + " ") But i have another problem with that line it seems. NETSET = "NetSet" + LBRACE + ZeroOrMore(Group(v_text | v_num | NETSET_PLIST | NETSET_NLIST)) + RBRACE It's referencing to NETSET_PLIST which is defined as follows: NETSET_PLIST = ( "list" + EQ + LBRACE + ZeroOrMore(NETENTRY) + RBRACE ) Which means it should match "list={" and a few child options gut i get following message while parsing: pyparsing.ParseException: Expected "}" (at char 302), (line:8, col:25) Claus Am 18.09.2012 14:22:46, schrieb Paul McGuire: > In your definition of 'text', you only match a single word - "name={LAN > SIDE}" contains a text value with whitespace, more than one word. Also, > beware of making 'text' match too much, you may end up including your > trailing '}' as part of the text (as you will with your current definition > of text as "Word(printables)"). > > -- Paul > > > -----Original Message----- > From: Claus Rosenberger [mailto:> cla...@ro...> ] > Sent: Tuesday, September 18, 2012 3:13 AM > To: > pyp...@li... > Subject: [Pyparsing] Parsing object structure > > Hi, > > i try to parse an object structure which is using open and close tags > and containing lists of another objects. > > Example: > > NetSet{ > name={LAN SIDE} > oid={123998333,723663,2625521122} > readOnly={0} > origin={} > global={0} > comment={Voice Server UC} > list={ > NetEntry{ > name={} > readOnly={0} > origin={} > global={0} > comment={} > addr={192.169.0.0/24} > } > } > neglist={ > } > } > > > I tried to start with following code structure and don't know if it > makes sense or not. > > from pyparsing import * > > LBRACE,RBRACE,SEMI,EQ,PCT = map(Suppress,"{};=%") > comment = SEMI + restOfLine > > keyName = Word(alphas) > text = Word( alphanums, "-", ",") > text = Word(printables) > num = Word(nums) > ip = Combine(Word(nums) + ('.' + Word(nums))*3) > > v_text = keyName + EQ + LBRACE + Optional(text) + RBRACE > v_num = keyName + EQ + LBRACE + Optional(num) + RBRACE > v_ip = keyName + EQ + LBRACE + Optional(ip) + RBRACE > > NETENTRY = ( "NetEntry" + LBRACE + ZeroOrMore(Group(v_text | v_num | > v_ip)) + RBRACE ) > NETSET_PLIST = ( "list" + EQ + LBRACE + ZeroOrMore(NETENTRY) + RBRACE ) > NETSET_NLIST = "neglist" + EQ + LBRACE + ZeroOrMore(Group(NETENTRY)) + > RBRACE > NETSET = "NetSet" + LBRACE + ZeroOrMore(Group(v_text | v_num | > NETSET_PLIST | NETSET_NLIST)) + RBRACE > rule_ref = NETSET > > for mr in rule_ref.parseFile("sample"): > print mr > > I get following result: > > pyparsing.ParseException: Expected "}" (at char 48), (line:2, col:25) > > It seems NETSET_PLIST does not work, perhaps my code is the wrong approach. > > Thanks a lot > Claus > > ---------------------------------------------------------------------------- > -- > Live Security Virtual Conference > Exclusive live event will cover all the ways today's security and > threat landscape has changed and how IT managers can respond. Discussions > will include endpoint security, mobile security and the latest in malware > threats. > http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users |
From: Claus R. <cla...@ro...> - 2012-09-25 10:00:46
|
Ok, i solved it, some chars were missing. Now i try to handle german umlauts, text = Word( alphas8bit + alphanums + " -,/.|:_()+;&" ) does not work, error message says /usr/lib/python2.7/dist-packages/pyparsing.py:1698: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal i try to mix it up with unicode as well text = Word( alphas8bit + alphanums + " -,/.|:_()+;&" + u"ü") but it does not work either. Is there any alphanums to use with unicode? Claus Am 20.09.2012 12:30:14, schrieb Claus Rosenberger: > Ok, i changed it to the minimal set i need at the moment. > text = Word( alphanums + "," + " ") > > > But i have another problem with that line it seems. > NETSET = "NetSet" + LBRACE + ZeroOrMore(Group(v_text | v_num | NETSET_PLIST | NETSET_NLIST)) + RBRACE > > > It's referencing to NETSET_PLIST which is defined as follows: > NETSET_PLIST = ( "list" + EQ + LBRACE + ZeroOrMore(NETENTRY) + RBRACE ) > > > Which means it should match "list={" and a few child options gut i get following message while parsing: > pyparsing.ParseException: Expected "}" (at char 302), (line:8, col:25) > > > Claus |