473,287 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

help with pyparsing

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

I have the following lines that I would like to parse in python using
pyparsing, but have some problems forming the grammar.

Line in file:
table <ALINKconst { 207.135.103.128/26, 207.135.112.64/29 }
table <INTRANETpersist { ! 10.200.2/24, 10.200/22 }
table <RFC_1918const { 192.168/16, ! 172.24.1/29, 172.16/12, 169.254/16 }
table <DIALERpersist { 10.202/22 }
table <RAVPNconst { 10.206/22 }
table <KSconst { \
10.205.1/24, \
169.136.241.68, \
169.136.241.70, \
169.136.241.71, \
169.136.241.72, \
169.136.241.75, \
169.136.241.76, \
169.136.241.77, \
169.136.241.78, \
169.136.241.79, \
169.136.241.81, \
169.136.241.82, \
169.136.241.85 }

I have the following grammar defn.

tableName = Word(alphanums + "-" + "_")
leftClose = Suppress("<")
rightClose = Suppress(">")
key = Suppress("table")
tableType = Regex("persist|const")
ip4Address = OneOrMore(Word(nums + "."))
ip4Network = Group(ip4Address + Optional(Word("/") +
OneOrMore(Word(nums))))
temp = ZeroOrMore("\\" + "\n")
tableList = OneOrMore(Optional("\\") |
ip4Network | ip4Address | Suppress(",") | Literal("!"))
leftParen = Suppress("{")
rightParen = Suppress("}")

table = key + leftClose + tableName + rightClose + tableType + \
leftParen + tableList + rightParen

I cannot seem to match sixth line in the file above, i.e table name with
KS, how do I form the grammar for it, BTW, I still cannot seem to ignore
comments using table.ignore(Literal("#") + restOfLine), I get a parse error.

Any help appreciated.
Thanks
Prabhu
- -
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHXMhFTkjpaeKzB9YRAmZYAJ9Lyys6+xCrGEsyy33AoR WVdUOXQwCfTG9Q
/f7JZ2pAW6WDSzs79jbDFQE=
=CGb0
-----END PGP SIGNATURE-----
Dec 10 '07 #1
3 2470
On Dec 10, 7:01 am, Prabhu Gurumurthy <pguru...@gmail.comwrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

I have the following lines that I would like to parse in python using
pyparsing, but have some problems forming the grammar.

Line in file:
table <ALINKconst { 207.135.103.128/26, 207.135.112.64/29 }
table <INTRANETpersist { ! 10.200.2/24, 10.200/22 }
table <RFC_1918const { 192.168/16, ! 172.24.1/29, 172.16/12, 169.254/16 }
table <DIALERpersist { 10.202/22 }
table <RAVPNconst { 10.206/22 }
table <KSconst { \
10.205.1/24, \
169.136.241.68, \
169.136.241.70, \
169.136.241.71, \
169.136.241.72, \
169.136.241.75, \
169.136.241.76, \
169.136.241.77, \
169.136.241.78, \
169.136.241.79, \
169.136.241.81, \
169.136.241.82, \
169.136.241.85 }

I have the following grammar defn.

tableName = Word(alphanums + "-" + "_")
leftClose = Suppress("<")
rightClose = Suppress(">")
key = Suppress("table")
tableType = Regex("persist|const")
ip4Address = OneOrMore(Word(nums + "."))
ip4Network = Group(ip4Address + Optional(Word("/") +
OneOrMore(Word(nums))))
temp = ZeroOrMore("\\" + "\n")
tableList = OneOrMore(Optional("\\") |
ip4Network | ip4Address | Suppress(",") | Literal("!"))
leftParen = Suppress("{")
rightParen = Suppress("}")

table = key + leftClose + tableName + rightClose + tableType + \
leftParen + tableList + rightParen

I cannot seem to match sixth line in the file above, i.e table name with
KS, how do I form the grammar for it, BTW, I still cannot seem to ignore
comments using table.ignore(Literal("#") + restOfLine), I get a parse error.

Any help appreciated.
Thanks
Prabhu
- -
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE -http://enigmail.mozdev.org

iD8DBQFHXMhFTkjpaeKzB9YRAmZYAJ9Lyys6+xCrGEsyy33AoR WVdUOXQwCfTG9Q
/f7JZ2pAW6WDSzs79jbDFQE=
=CGb0
-----END PGP SIGNATURE-----
Shouldn't tableList be 'ZeroOrMore' instead...
Dec 10 '07 #2
On Dec 9, 11:01 pm, Prabhu Gurumurthy <pguru...@gmail.comwrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

I have the following lines that I would like to parse in python using
pyparsing, but have some problems forming the grammar.

Line in file:
table <ALINKconst { 207.135.103.128/26, 207.135.112.64/29 }
table <INTRANETpersist { ! 10.200.2/24, 10.200/22 }
table <RFC_1918const { 192.168/16, ! 172.24.1/29, 172.16/12, 169.254/16 }
table <DIALERpersist { 10.202/22 }
table <RAVPNconst { 10.206/22 }
table <KSconst { \
10.205.1/24, \
169.136.241.68, \
169.136.241.70, \
169.136.241.71, \
169.136.241.72, \
169.136.241.75, \
169.136.241.76, \
169.136.241.77, \
169.136.241.78, \
169.136.241.79, \
169.136.241.81, \
169.136.241.82, \
169.136.241.85 }

I have the following grammar defn.

tableName = Word(alphanums + "-" + "_")
leftClose = Suppress("<")
rightClose = Suppress(">")
key = Suppress("table")
tableType = Regex("persist|const")
ip4Address = OneOrMore(Word(nums + "."))
ip4Network = Group(ip4Address + Optional(Word("/") +
OneOrMore(Word(nums))))
temp = ZeroOrMore("\\" + "\n")
tableList = OneOrMore(Optional("\\") |
ip4Network | ip4Address | Suppress(",") | Literal("!"))
leftParen = Suppress("{")
rightParen = Suppress("}")

table = key + leftClose + tableName + rightClose + tableType + \
leftParen + tableList + rightParen

I cannot seem to match sixth line in the file above, i.e table name with
KS, how do I form the grammar for it, BTW, I still cannot seem to ignore
comments using table.ignore(Literal("#") + restOfLine), I get a parse error.

Any help appreciated.
Thanks
Prabhu
Prabhu -

This is a good start, but here are some suggestions:

1. ip4Address = OneOrMore(Word(nums + "."))

Word(nums+".") will read any contiguous set of characters in the
string nums+".", so OneOrMore is not necessary for reading in an
ip4Address. Just use:

ip4Address = Word(nums + ".")
2. ip4Network = Group(ip4Address + Optional(Word("/") +
OneOrMore(Word(nums))))

Same comment, OneOrMore is not needed for the added value to the
ip4Address:

ip4Network = Group(ip4Address + Optional(Word("/") + Word(nums))))
3. tableList = OneOrMore(Optional("\\") |
ip4Network | ip4Address | Suppress(",") |
Literal("!"))

The list of ip4Networks is just a comma-delimited list, with some
entries preceded with a '!' character. It is simpler to use
pyparsing's built-in helper, delimitedList, as in:

tableList = Group( delimitedList(Group("!"+ip4Network)|ip4Network) )
Yes, I know, you are saying, "but what about all those backslashes?"
The backslashes look like they are just there as line continuations.
We can define an ignore expression, so that the table expression, and
all of its contained expressions, will ignore '\' characters as line
continuations:

table.ignore( Literal("\\") + LineEnd() )

And I'm not sure why you had trouble with ignoring '#' + restOfLine,
it works fine in the program below.

If you make these changes, your program will look something like this:

tableName = Word(alphanums + "-" + "_")
leftClose = Suppress("<")
rightClose = Suppress(">")
key = Suppress("table")
tableType = Regex("persist|const")
ip4Address = Word(nums + ".")
ip4Network = Group(ip4Address + Optional(Word("/") + Word(nums)))
tableList = Group(delimitedList(Group("!"+ip4Network)|ip4Netwo rk))
leftParen = Suppress("{")
rightParen = Suppress("}")

table = key + leftClose + tableName + rightClose + tableType + \
leftParen + tableList + rightParen
table.ignore(Literal("\\") + LineEnd())
table.ignore(Literal("#") + restOfLine)

# parse the input line, and pprint the results
result = OneOrMore(table).parseString(line)
from pprint import pprint
pprint(result.asList())

Prints out:
['ALINK',
'const',
[['207.135.103.128', '/', '26'], ['207.135.112.64', '/', '29']],
'INTRANET',
'persist',
[['!', ['10.200.2', '/', '24']], ['10.200', '/', '22']],
'RFC_1918',
'const',
[['192.168', '/', '16'],
['!', ['172.24.1', '/', '29']],
['172.16', '/', '12'],
['169.254', '/', '16']],
'DIALER',
'persist',
[['10.202', '/', '22']],
'RAVPN',
'const',
[['10.206', '/', '22']],
'KS',
'const',
[['10.205.1', '/', '24'],
['169.136.241.68'],
['169.136.241.70'],
['169.136.241.71'],
['169.136.241.72'],
['169.136.241.75'],
['169.136.241.76'],
['169.136.241.77'],
['169.136.241.78'],
['169.136.241.79'],
['169.136.241.81'],
['169.136.241.82'],
['169.136.241.85']]]

-- Paul
Dec 10 '07 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Paul McGuire wrote:
On Dec 9, 11:01 pm, Prabhu Gurumurthy <pguru...@gmail.comwrote:
>-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

I have the following lines that I would like to parse in python using
pyparsing, but have some problems forming the grammar.

Line in file:
table <ALINKconst { 207.135.103.128/26, 207.135.112.64/29 }
table <INTRANETpersist { ! 10.200.2/24, 10.200/22 }
table <RFC_1918const { 192.168/16, ! 172.24.1/29, 172.16/12, 169.254/16 }
table <DIALERpersist { 10.202/22 }
table <RAVPNconst { 10.206/22 }
table <KSconst { \
10.205.1/24, \
169.136.241.68, \
169.136.241.70, \
169.136.241.71, \
169.136.241.72, \
169.136.241.75, \
169.136.241.76, \
169.136.241.77, \
169.136.241.78, \
169.136.241.79, \
169.136.241.81, \
169.136.241.82, \
169.136.241.85 }

I have the following grammar defn.

tableName = Word(alphanums + "-" + "_")
leftClose = Suppress("<")
rightClose = Suppress(">")
key = Suppress("table")
tableType = Regex("persist|const")
ip4Address = OneOrMore(Word(nums + "."))
ip4Network = Group(ip4Address + Optional(Word("/") +
OneOrMore(Word(nums))))
temp = ZeroOrMore("\\" + "\n")
tableList = OneOrMore(Optional("\\") |
ip4Network | ip4Address | Suppress(",") | Literal("!"))
leftParen = Suppress("{")
rightParen = Suppress("}")

table = key + leftClose + tableName + rightClose + tableType + \
leftParen + tableList + rightParen

I cannot seem to match sixth line in the file above, i.e table name with
KS, how do I form the grammar for it, BTW, I still cannot seem to ignore
comments using table.ignore(Literal("#") + restOfLine), I get a parse error.

Any help appreciated.
Thanks
Prabhu

Prabhu -

This is a good start, but here are some suggestions:

1. ip4Address = OneOrMore(Word(nums + "."))

Word(nums+".") will read any contiguous set of characters in the
string nums+".", so OneOrMore is not necessary for reading in an
ip4Address. Just use:

ip4Address = Word(nums + ".")
2. ip4Network = Group(ip4Address + Optional(Word("/") +
OneOrMore(Word(nums))))

Same comment, OneOrMore is not needed for the added value to the
ip4Address:

ip4Network = Group(ip4Address + Optional(Word("/") + Word(nums))))
3. tableList = OneOrMore(Optional("\\") |
ip4Network | ip4Address | Suppress(",") |
Literal("!"))

The list of ip4Networks is just a comma-delimited list, with some
entries preceded with a '!' character. It is simpler to use
pyparsing's built-in helper, delimitedList, as in:

tableList = Group( delimitedList(Group("!"+ip4Network)|ip4Network) )
Yes, I know, you are saying, "but what about all those backslashes?"
The backslashes look like they are just there as line continuations.
We can define an ignore expression, so that the table expression, and
all of its contained expressions, will ignore '\' characters as line
continuations:

table.ignore( Literal("\\") + LineEnd() )

And I'm not sure why you had trouble with ignoring '#' + restOfLine,
it works fine in the program below.

If you make these changes, your program will look something like this:

tableName = Word(alphanums + "-" + "_")
leftClose = Suppress("<")
rightClose = Suppress(">")
key = Suppress("table")
tableType = Regex("persist|const")
ip4Address = Word(nums + ".")
ip4Network = Group(ip4Address + Optional(Word("/") + Word(nums)))
tableList = Group(delimitedList(Group("!"+ip4Network)|ip4Netwo rk))
leftParen = Suppress("{")
rightParen = Suppress("}")

table = key + leftClose + tableName + rightClose + tableType + \
leftParen + tableList + rightParen
table.ignore(Literal("\\") + LineEnd())
table.ignore(Literal("#") + restOfLine)

# parse the input line, and pprint the results
result = OneOrMore(table).parseString(line)
from pprint import pprint
pprint(result.asList())

Prints out:
['ALINK',
'const',
[['207.135.103.128', '/', '26'], ['207.135.112.64', '/', '29']],
'INTRANET',
'persist',
[['!', ['10.200.2', '/', '24']], ['10.200', '/', '22']],
'RFC_1918',
'const',
[['192.168', '/', '16'],
['!', ['172.24.1', '/', '29']],
['172.16', '/', '12'],
['169.254', '/', '16']],
'DIALER',
'persist',
[['10.202', '/', '22']],
'RAVPN',
'const',
[['10.206', '/', '22']],
'KS',
'const',
[['10.205.1', '/', '24'],
['169.136.241.68'],
['169.136.241.70'],
['169.136.241.71'],
['169.136.241.72'],
['169.136.241.75'],
['169.136.241.76'],
['169.136.241.77'],
['169.136.241.78'],
['169.136.241.79'],
['169.136.241.81'],
['169.136.241.82'],
['169.136.241.85']]]

-- Paul
Awesome, thanks a lot will try it today and will let you know how it
proceeds.

thanks again.
Prabhu
- -
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHXWOQTkjpaeKzB9YRAq/aAJ9b0uocbP+1XxIVj4LgS76uFEuQHwCgxojY
zv05Raaj5McSEzDWXiSxf9c=
=MMFV
-----END PGP SIGNATURE-----
Dec 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: RiGGa | last post by:
Hi, I want to parse a web page in Python and have it write certain values out to a mysql database. I really dont know where to start with parsing the html code ( I can work out the database...
5
by: Lukas Holcik | last post by:
Hi everyone! How can I simply search text for regexps (lets say <a href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a dictionary { name : URL}? In a single pass if it could....
15
by: kpp9c | last post by:
I am kind of in a bit of a jam (okay a big jam) and i was hoping that someone here could give me a quick hand. I had a few pages of time calculations to do. So, i just started in on them typing...
8
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me...
5
by: Dave | last post by:
OK, I'm stumped. I'm trying to find newline characters (\n, specifically) that are NOT in comments. So, for example (where "<-" = a newline character):...
6
by: GHUM | last post by:
I need to split a text at every ; (Semikolon), but not at semikolons which are "escaped" within a pair of $$ or $_$ signs. My guess was that something along this should happen withing csv.py;...
1
by: Steve | last post by:
Hi All (especially Paul McGuire!) Could you lend a hand in the grammar and paring of the output from the function win32pdhutil.ShowAllProcesses()? This is the code that I have so far (it is...
1
by: Neal Becker | last post by:
I'm just trying out pyparsing. I get stack overflow on my first try. Any help? #/usr/bin/python from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList first_line = ''...
7
by: Donn Ingle | last post by:
Hi, I really hope someone can help me -- I'm stuck. I have written three versions of code over a week and still can't get past this problem, it's blocking my path to getting other code written. ...
5
by: Paul McGuire | last post by:
I've just uploaded to SourceForge and PyPI the latest update to pyparsing, version 1.5.1. It has been a couple of months since 1.5.0 was released, and a number of bug-fixes and enhancements have...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.