473,406 Members | 2,713 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,406 software developers and data experts.

pyparsing problem

Hi,

I try to parse a file with pyparsing and get this output:

['generic', 'host-01', 'host alias xyz', '10.0.0.1']
- alias: host alias xyz
- host_name: ['host-01']
- ip_address: ['10.0.0.1']
- use: ['generic']
<Hosts>
<ITEM>generic</ITEM>
<host_name>host-01</host_name>
<alias>host alias xyz</alias>
<ip_address>10.0.0.1</ip_address>
</Hosts>

['generic', 'host-01', 'host alias xyz', '10.0.0.1']

finished

What I don't understand is why I get the line

<ITEM>generic</ITEM>

and not

<use>generic</use>

as there is an associated name in the dump() output.

Thank you very much in advance for any clue or help you could
provide.

The code:
---------

#!/usr/bin/env python

from pyparsing import *

sample = """
define host{
use generic
host_name host-01
alias host alias xyz
address 10.0.0.1
}
"""

# define tokens
LBRACE,RBRACE = map(Suppress, '{}')
ipAddress = Combine(Word(nums) + ('.' + Word(nums))*3)
useTemplate = oneOf('generic user')

# define grammar

deviceName = Word(alphanums+'-')
hostDef = Literal('define').suppress() + Literal('host').suppress()
useLine = Suppress('use') + useTemplate + Suppress(SkipTo(lineEnd))
host_nameLine = Suppress('host_name') + deviceName + Suppress(SkipTo
(lineEnd))
aliasLine = Suppress('alias') + SkipTo(lineEnd)
aliasLine.setParseAction(lambda x: ' '.join(x[0].split()))
ip_addressLine = Suppress('address') + ipAddress

use = useLine.setResultsName('use')
host_name = host_nameLine.setResultsName('host_name')
alias = aliasLine.setResultsName('alias')
ip_address = ip_addressLine.setResultsName('ip_address')
host_stmt = (use + host_name + alias + ip_address)
host = hostDef + LBRACE + host_stmt + RBRACE

test_file = OneOrMore(host) + stringEnd
# test
x = test_file.parseString(sample)
print x.dump()
print
print x.asXML('Hosts')
print
print x.asList()
print

print 'finished'

Jul 1 '08 #1
3 1273
On Jul 1, 8:02*am, name <x...@y.zwrote:
Hi,

I try to parse a file with pyparsing and get this output:

* * ['generic', 'host-01', 'host alias xyz', '10.0.0.1']
* * - alias: host alias xyz
* * - host_name: ['host-01']
* * - ip_address: ['10.0.0.1']
* * - use: ['generic']

* * <Hosts>
* * * <ITEM>generic</ITEM>
* * * <host_name>host-01</host_name>
* * * <alias>host alias xyz</alias>
* * * <ip_address>10.0.0.1</ip_address>
* * </Hosts>

* * ['generic', 'host-01', 'host alias xyz', '10.0.0.1']

* * finished

What I don't understand is why I get the line

* * <ITEM>generic</ITEM>

and not

* * <use>generic</use>

as there is an associated name in the dump() output.

Thank you very much in advance for any clue or help you could
provide.

The code:
---------

#!/usr/bin/env python

from pyparsing import *

sample = """
define host{
* use * * * * * * * * * generic
* host_name * * * * * * host-01
* alias * * * * * * * * host alias xyz
* address * * * * * * * * * * * 10.0.0.1}

"""

# define tokens
LBRACE,RBRACE = map(Suppress, '{}')
ipAddress = Combine(Word(nums) + ('.' + Word(nums))*3)
useTemplate = oneOf('generic user')

# define grammar

deviceName = Word(alphanums+'-')
hostDef = Literal('define').suppress() + Literal('host').suppress()
useLine = Suppress('use') + useTemplate + Suppress(SkipTo(lineEnd))
host_nameLine = Suppress('host_name') + deviceName + Suppress(SkipTo
(lineEnd))
aliasLine = Suppress('alias') + SkipTo(lineEnd)
aliasLine.setParseAction(lambda x: ' '.join(x[0].split()))
ip_addressLine = Suppress('address') + ipAddress

use = useLine.setResultsName('use')
host_name = host_nameLine.setResultsName('host_name')
alias = aliasLine.setResultsName('alias')
ip_address = ip_addressLine.setResultsName('ip_address')

host_stmt = (use + host_name + alias + ip_address)
host = hostDef + LBRACE + host_stmt *+ RBRACE

test_file = OneOrMore(host) + stringEnd

# test
x = test_file.parseString(sample)
print x.dump()
print
print x.asXML('Hosts')
print
print x.asList()
print

print 'finished'
Looks like this is a bug in asXML(). Note that if I reverse the use
and host_name strings in the input and in your grammar, I get this XML
output:

<Hosts>
<ITEM>host-01</ITEM>
<use>generic</use>
<alias>host alias xyz</alias>
<ip_address>10.0.0.1</ip_address>
</Hosts>

Fortunately, you have provided a nice short test case, which should
allow me to track down the problem.

Thanks,
-- Paul
Jul 1 '08 #2
Paul McGuire <pt***@austin.rr.comwrote in news:be7af822-70d7-44fb-96fa-
78**********@r66g2000hsg.googlegroups.com:
>
Looks like this is a bug in asXML(). Note that if I reverse the use
and host_name strings in the input and in your grammar, I get this XML
output:

<Hosts>
<ITEM>host-01</ITEM>
<use>generic</use>
<alias>host alias xyz</alias>
<ip_address>10.0.0.1</ip_address>
</Hosts>

Fortunately, you have provided a nice short test case, which should
allow me to track down the problem.

Thanks,
-- Paul
You are welcome! And I would like to thank you for this outstanding tool!

Thanks,
Bernhard
Jul 1 '08 #3
On Jul 1, 8:02*am, name <x...@y.zwrote:
Hi,

I try to parse a file with pyparsing and get this output:

* * ['generic', 'host-01', 'host alias xyz', '10.0.0.1']
* * - alias: host alias xyz
* * - host_name: ['host-01']
* * - ip_address: ['10.0.0.1']
* * - use: ['generic']

* * <Hosts>
* * * <ITEM>generic</ITEM>
* * * <host_name>host-01</host_name>
* * * <alias>host alias xyz</alias>
* * * <ip_address>10.0.0.1</ip_address>
* * </Hosts>

* * ['generic', 'host-01', 'host alias xyz', '10.0.0.1']

* * finished

What I don't understand is why I get the line

* * <ITEM>generic</ITEM>

and not

* * <use>generic</use>

as there is an associated name in the dump() output.

Thank you very much in advance for any clue or help you could
provide.

The code:
---------

#!/usr/bin/env python

from pyparsing import *

sample = """
define host{
* use * * * * * * * * * generic
* host_name * * * * * * host-01
* alias * * * * * * * * host alias xyz
* address * * * * * * * * * * * 10.0.0.1}

"""

# define tokens
LBRACE,RBRACE = map(Suppress, '{}')
ipAddress = Combine(Word(nums) + ('.' + Word(nums))*3)
useTemplate = oneOf('generic user')

# define grammar

deviceName = Word(alphanums+'-')
hostDef = Literal('define').suppress() + Literal('host').suppress()
useLine = Suppress('use') + useTemplate + Suppress(SkipTo(lineEnd))
host_nameLine = Suppress('host_name') + deviceName + Suppress(SkipTo
(lineEnd))
aliasLine = Suppress('alias') + SkipTo(lineEnd)
aliasLine.setParseAction(lambda x: ' '.join(x[0].split()))
ip_addressLine = Suppress('address') + ipAddress

use = useLine.setResultsName('use')
host_name = host_nameLine.setResultsName('host_name')
alias = aliasLine.setResultsName('alias')
ip_address = ip_addressLine.setResultsName('ip_address')

host_stmt = (use + host_name + alias + ip_address)
host = hostDef + LBRACE + host_stmt *+ RBRACE

test_file = OneOrMore(host) + stringEnd

# test
x = test_file.parseString(sample)
print x.dump()
print
print x.asXML('Hosts')
print
print x.asList()
print

print 'finished'
Try downloading the latest pyparsing.py file from the SourceForge SVN
repository. This bug should be fixed in the latest version.

-- Paul
Jul 2 '08 #4

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

Similar topics

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....
4
by: the.theorist | last post by:
Hey, I'm trying my hand and pyparsing a log file (named l.log): FIRSTLINE PROPERTY1 DATA1 PROPERTY2 DATA2 PROPERTYS LIST ID1 data1 ID2 data2
4
by: Bytter | last post by:
Hi, I'm trying to construct a parser, but I'm stuck with some basic stuff... For example, I want to match the following: letter = "A"..."Z" | "a"..."z" literal = letter+ include_bool := "+"...
13
by: 7stud | last post by:
To the developer: 1) I went to the pyparsing wiki to download the pyparsing module and try it 2) At the wiki, there was no index entry in the table of contents for Downloads. After searching...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
2
by: Nathan Harmston | last post by:
Hi, I know this isnt the pyparsing list, but it doesnt seem like there is one. I m trying to use pyparsing to parse a file however I cant get the Optional keyword to work. My file generally...
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...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
4
by: Marek Kubica | last post by:
Hi, I am trying to get this stuff working, but I still fail. I have a format which consists of three elements: \d{4}M?-\d (4 numbers, optional M, dash, another number) EMPTY (the <EMPTYtoken)...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.