Discussion:
[Pyparsing] delimitedList Results
Loïc Berthe
2014-04-09 17:07:37 UTC
Permalink
Hi,

I have some questions on how to use properly parseResults associated with a
delimitedList.

Here is the kind of line I would like to parse :

[code]
string = 'line 22 width=2;type=1 V101-,V103,V99+,V12 L102'
[/code]

And here is the parser I've built to extract information from this string:

[code]
import pyparsing as p


integer = p.Word(p.nums)
name = p.Word(p.alphas, p.alphanums+'_')
ident = p.Combine(name('id') + p.Optional(p.oneOf('- +')))

lineParser = ( 'line' + integer('num')
+ 'width=' + integer('width')+';'
+ 'type=' + integer('type')
+ p.delimitedList(ident)('points')
+ name('name')
)

r = lineParser.parseString(string)
[/code]
print r.name
L102

but with delimitedList, it seems to be a bit more trickier as the associated
parseResults can not be used as a list of ParseResults but rather as a list of
print type(pt), pt
<type 'str'> V101-
<type 'str'> V103
<type 'str'> V99+
<type 'str'> V12

I can get the list of point directly with r.points.asList(), but I don't know
how to access to the list of ids.

I was expecting that I could use classical __getitem__ method to list all ids
with something like [ pt.id for pt in r.points]
Is there any way to do this ?
print repr(r.points)
(['V101-', 'V103', 'V99+', 'V12'], {'id': [('V101', 0), ('V103', 1), ('V99',
2), ('V12', 3)]})

it seems that the id key is associated with the list that I m looking for, but
r.points.id
V12

Have you got any clue?


Regards
--
Loïc
Paul McGuire
2014-04-09 18:23:06 UTC
Permalink
In place of:

ident = p.Combine(name('id') + p.Optional(p.oneOf('- +')))

use:

ident = p.Group(name('id') + p.Optional(p.oneOf('- +')))

The Group class is used to create sub-structures within a parsed result.
Otherwise, the default behavior for pyparsing is to just create a single
list of strings.

The reason you see all the id's in the internal directory for the
ParseResults is that it is possible to define a results name that is
actually an accumulator of all matching strings. When using the full call
syntax, add the listAllMatches argument, as in:

expr.setResultsName("id", listAllMatches=True)

When using the abbreviated call syntax as you have done, use:

expr("id*")

But based on what you have so far, I think Group is really the way to go. I
would also add a name for the optional trailing '+' or '-' sign, as in:

ident = p.Group(name('id') + p.Optional(p.oneOf('- +')('sign')))

Then you can write:

for pt in r.points:
if 'sign' in pt: # or "if pt.sign:"
# do special sign stuff with pt.sign



-- Paul

-----Original Message-----
From: Loïc Berthe [mailto:***@lilotux.net]
Sent: Wednesday, April 09, 2014 12:08 PM
To: pyparsing-***@lists.sourceforge.net
Subject: [Pyparsing] delimitedList Results

Hi,

I have some questions on how to use properly parseResults associated with a
delimitedList.

Here is the kind of line I would like to parse :

[code]
string = 'line 22 width=2;type=1 V101-,V103,V99+,V12 L102'
[/code]

And here is the parser I've built to extract information from this string:

[code]
import pyparsing as p


integer = p.Word(p.nums)
name = p.Word(p.alphas, p.alphanums+'_')
ident = p.Combine(name('id') + p.Optional(p.oneOf('- +')))

lineParser = ( 'line' + integer('num')
+ 'width=' + integer('width')+';'
+ 'type=' + integer('type')
+ p.delimitedList(ident)('points')
+ name('name')
)

r = lineParser.parseString(string)
[/code]
print r.name
L102

but with delimitedList, it seems to be a bit more trickier as the associated
parseResults can not be used as a list of ParseResults but rather as a list
of
print type(pt), pt
<type 'str'> V101-
<type 'str'> V103
<type 'str'> V99+
<type 'str'> V12

I can get the list of point directly with r.points.asList(), but I don't
know how to access to the list of ids.

I was expecting that I could use classical __getitem__ method to list all
ids with something like [ pt.id for pt in r.points] Is there any way to do
this ?
print repr(r.points)
(['V101-', 'V103', 'V99+', 'V12'], {'id': [('V101', 0), ('V103', 1), ('V99',
2), ('V12', 3)]})

it seems that the id key is associated with the list that I m looking for,
r.points.id
V12

Have you got any clue?


Regards
--
Loïc

----------------------------------------------------------------------------
--
Put Bad Developers to Shame
Dominate Development with Jenkins Continuous Integration Continuously
Automate Build, Test & Deployment Start a new project now. Try Jenkins in
the cloud.
http://p.sf.net/sfu/13600_Cloudbees
_______________________________________________
Pyparsing-users mailing list
Pyparsing-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyparsing-users


---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com

Loading...