473,560 Members | 2,918 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More fun with PyParsing - almost did it on my own..

Hi all,

I almost did my first pyparsing without help but here we go again.
Let's start with my code. The sample data is listed below.

# This will gather the following ( "NamedPin" "PinDirecti on"
"OptionalSignal " )
guts = Group( LPAR.suppress() +
quotedString.se tParseAction(re moveQuotes).set ResultsName("na me") +
quotedString.se tParseAction(re moveQuotes).set ResultsName("di rection")
+
Optional(quoted String.setParse Action(removeQu otes).setResult sName("signal") )
+ RPAR.suppress() )

# This will simply wrap the Cell Info around it
cell = Group( Literal("dbSetC ellPortTypes"). suppress() +
quotedString.se tParseAction(re moveQuotes).set ResultsName("li brary") +
quotedString.se tParseAction(re moveQuotes).set ResultsName("na me") +
Literal("'").su ppress() + LPAR.suppress() +
OneOrMore(guts) .setResultsName ("pins") + RPAR.suppress() ) +
Literal("#f").s uppress() | Literal("#t").s uppress()

# This grabs many cells
cells = OneOrMore(cell)

OK and it sorta works if I do the following:

x = cells.parseStri ng(data)
print x[0].asDict()

reveals
{'pins': ([(['A', 'Input'], {'direction': [('Input', 1)], 'name':
[('A', 0)]}), (['B', 'Input'], {'direction': [('Input', 1)], 'name':
[('B', 0)]}), (['Y', 'Output'], {'direction': [('Output', 1)], 'name':
[('Y', 0)]}), (['VDD', 'Inout', 'Power'], {'direction': [('Inout',
1)], 'name': [('VDD', 0)], 'signal': [('Power', 2)]}), (['VSS',
'Inout', 'Ground'], {'direction': [('Inout', 1)], 'name': [('VSS',
0)], 'signal': [('Ground', 2)]})], {}), 'name': 'AND2X1', 'library':
'stdcell130'}

As you can see the Pins is all jacked up and I want is not that. I
want the following

{ 'name': 'AND2X1',
'library':'stdc ell130'
'pins': [ { 'name': 'VSS', 'direction':'In out', 'signal':'Groun d'},
{ 'name': 'VDD', 'direction':'In out', 'signal':'Power '},
{ 'name': 'A', 'direction':'In put' },
{ 'name': 'B', 'direction':'In put' },
{ 'name': 'Y', 'direction':'Ou tput' } ]
}

What did I do wrong in my code..

Thanks again!

]


I would expect my results to look like this:

But to get any info I must do this

print x[0].asDict()

which is not really what want.
What I expect is this:
[
data = """dbSetCellPor tTypes "stdcell130 " "AND2X1" '(
("A" "Input" )
("B" "Input" )
("Y" "Output" )
("VDD" "Inout" "Power" )
("VSS" "Inout" "Ground" )
) #f
dbSetCellPortTy pes "stdcell130 " "AND2X2" '(
("A" "Input" )
("B" "Input" )
("Y" "Output" )
("VDD" "Inout" "Power" )
("VSS" "Inout" "Ground" )
) #f """
Jun 27 '08 #1
1 1673
On May 14, 6:07*pm, rh0dium <steven.kl...@g mail.comwrote:
Hi all,

I almost did my first pyparsing without help but here we go again.
Let's start with my code. *The sample data is listed below.

<snip...>

x = *cells.parseStr ing(data)
print x[0].asDict()

reveals
{'pins': ([(['A', 'Input'], {'direction': [('Input', 1)], 'name':
[('A', 0)]}), (['B', 'Input'], {'direction': [('Input', 1)], 'name':
[('B', 0)]}), (['Y', 'Output'], {'direction': [('Output', 1)], 'name':
[('Y', 0)]}), (['VDD', 'Inout', 'Power'], {'direction': [('Inout',
1)], 'name': [('VDD', 0)], 'signal': [('Power', 2)]}), (['VSS',
'Inout', 'Ground'], {'direction': [('Inout', 1)], 'name': [('VSS',
0)], 'signal': [('Ground', 2)]})], {}), 'name': 'AND2X1', 'library':
'stdcell130'}

As you can see the Pins is all jacked up and I want is not that. *I
want the following

{ 'name': 'AND2X1',
* 'library':'stdc ell130'
* 'pins': [ { 'name': 'VSS', 'direction':'In out', 'signal':'Groun d'},
* * * * * * *{ 'name': 'VDD', 'direction':'In out', 'signal':'Power '},
* * * * * * *{ 'name': 'A', 'direction':'In put' },
* * * * * * *{ 'name': 'B', 'direction':'In put' },
* * * * * * *{ 'name': 'Y', 'direction':'Ou tput' } ]

}

What did I do wrong in my code..
Not a thing! asDict() is just not very good at dumping out lists of
subdicts. Look at the output when you iterate over the cells in x,
and the pins in each cell:

for cell in x:
print "Name:", cell["name"]
print "Library:", cell["library"]
print "Pins:"
for pin in cell["pins"]:
print pin.asDict()
print

Prints:

Name: AND2X1
Library: stdcell130
Pins:
{'direction': 'Input', 'name': 'A'}
{'direction': 'Input', 'name': 'B'}
{'direction': 'Output', 'name': 'Y'}
{'direction': 'Inout', 'name': 'VDD', 'signal': 'Power'}
{'direction': 'Inout', 'name': 'VSS', 'signal': 'Ground'}

Name: AND2X2
Library: stdcell130
Pins:
{'direction': 'Input', 'name': 'A'}
{'direction': 'Input', 'name': 'B'}
{'direction': 'Output', 'name': 'Y'}
{'direction': 'Inout', 'name': 'VDD', 'signal': 'Power'}
{'direction': 'Inout', 'name': 'VSS', 'signal': 'Ground'}
Now, here is a real trick. Each pin has a unique name, and the
collection of pins can be used to define a dict using the pin names as
dynamically-defined keys. You've laid all the ground work, all that
is needed is to define your sequence of OneOrMore(guts) as a dict
using the guts names as keys. The only change needed is to wrap this
OneOrMore(guts) in a pyparsing Dict class - that is, change:

OneOrMore(guts) .setResultsName ("pins")

to:

Dict(OneOrMore( guts)).setResul tsName("pins")

Now, if you iterate over each cell, you can dump out its structure:

for cell in x:
print cell.dump()
print

Prints:

['stdcell130', 'AND2X1', [['A', 'Input'], ['B', 'Input'], ['Y',
'Output'], ['VDD', 'Inout', 'Power'], ['VSS', 'Inout', 'Ground']]]
- library: stdcell130
- name: AND2X1
- pins: [['A', 'Input'], ['B', 'Input'], ['Y', 'Output'], ['VDD',
'Inout', 'Power'], ['VSS', 'Inout', 'Ground']]
- A: Input
- B: Input
- VDD: ['Inout', 'Power']
- direction: Inout
- name: VDD
- signal: Power
- VSS: ['Inout', 'Ground']
- direction: Inout
- name: VSS
- signal: Ground
- Y: Output

['stdcell130', 'AND2X2', [['A', 'Input'], ['B', 'Input'], ['Y',
'Output'], ['VDD', 'Inout', 'Power'], ['VSS', 'Inout', 'Ground']]]
- library: stdcell130
- name: AND2X2
- pins: [['A', 'Input'], ['B', 'Input'], ['Y', 'Output'], ['VDD',
'Inout', 'Power'], ['VSS', 'Inout', 'Ground']]
- A: Input
- B: Input
- VDD: ['Inout', 'Power']
- direction: Inout
- name: VDD
- signal: Power
- VSS: ['Inout', 'Ground']
- direction: Inout
- name: VSS
- signal: Ground
- Y: Output

To flesh out all fields of all pins, I suggest you add a default value
for the optional signal entry, and set the results name on the
Optional wrapper, not the quoted string. Change:

+
Optional(quoted String.setParse Action(removeQu otes).setResult sName("signal") )

to:

+
Optional(quoted String.setParse Action(removeQu otes),default=" ").setResultsNa me("signal")

Now dump() called on each cell prints out:

['stdcell130', 'AND2X1', [['A', 'Input', ''], ['B', 'Input', ''],
['Y', 'Output', ''], ['VDD', 'Inout', 'Power'], ['VSS', 'Inout',
'Ground']]]
- library: stdcell130
- name: AND2X1
- pins: [['A', 'Input', ''], ['B', 'Input', ''], ['Y', 'Output', ''],
['VDD', 'Inout', 'Power'], ['VSS', 'Inout', 'Ground']]
- A: ['Input', '']
- direction: Input
- name: A
- signal:
- B: ['Input', '']
- direction: Input
- name: B
- signal:
- VDD: ['Inout', 'Power']
- direction: Inout
- name: VDD
- signal: Power
- VSS: ['Inout', 'Ground']
- direction: Inout
- name: VSS
- signal: Ground
- Y: ['Output', '']
- direction: Output
- name: Y
- signal:

Power
Input
['stdcell130', 'AND2X2', [['A', 'Input', ''], ['B', 'Input', ''],
['Y', 'Output', ''], ['VDD', 'Inout', 'Power'], ['VSS', 'Inout',
'Ground']]]
- library: stdcell130
- name: AND2X2
- pins: [['A', 'Input', ''], ['B', 'Input', ''], ['Y', 'Output', ''],
['VDD', 'Inout', 'Power'], ['VSS', 'Inout', 'Ground']]
- A: ['Input', '']
- direction: Input
- name: A
- signal:
- B: ['Input', '']
- direction: Input
- name: B
- signal:
- VDD: ['Inout', 'Power']
- direction: Inout
- name: VDD
- signal: Power
- VSS: ['Inout', 'Ground']
- direction: Inout
- name: VSS
- signal: Ground
- Y: ['Output', '']
- direction: Output
- name: Y
- signal:

You can use the nested names shown in the dump to access individual
bits of the parsed results:

for cell in x:
print cell.name
print cell.pins.keys( )
print cell.pins.VDD.s ignal
print cell.pins.A.dir ection
print

prints:

AND2X1
['A', 'Y', 'B', 'VDD', 'VSS']
Power
Input

AND2X2
['A', 'Y', 'B', 'VDD', 'VSS']
Power
Input

-- Paul
Jun 27 '08 #2

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

Similar topics

3
2248
by: phil_nospam_schmidt | last post by:
I am scanning text that has identifiers with a constant prefix string followed by alphanumerics and underscores. I can't figure out, using pyparsing, how to match for this. The example expression below seems to be looking for whitespace between the 'atod' and the rest of the identifier. identifier_atod = 'atod' + pp.Word('_' + pp.alphanums)...
4
2066
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
1
1856
by: Khoa Nguyen | last post by:
I am trying to come up with a grammar that describes the following: record = f1,f2,...,fn END_RECORD All the f(i) has to be in that order. Any f(i) can be absent (e.g. f1,,f3,f4,,f6 END_RECORD) Number of f(i)'s can vary. For example, the followings are allowed: f1,f2 END_RECORD f1,f2,,f4,,f6 END_RECORD Any suggestions?
3
1299
by: Ant | last post by:
I have a home-grown Wiki that I created as an excercise, with it's own wiki markup (actually just a clone of the Trac wiki markup). The wiki text parser I wrote works nicely, but makes heavy use of regexes, tags and stacks to parse the text. As such it is a bit of a mantainability nightmare - adding new wiki constructs can be a bit painful. ...
4
1586
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 := "+" | "-" term = literal
3
2189
by: Steven Bethard | last post by:
Within a larger pyparsing grammar, I have something that looks like:: wsj/00/wsj_0003.mrg When parsing this, I'd like to keep around both the full string, and the AAA_NNNN substring of it, so I'd like something like:: (, {}) How do I go about this? I was using something like::
13
2048
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 around a bit, I finally discovered a tiny link buried in some text at the top of the home page. 3) Link goes to sourceforge. At sourceforge, there...
1
2633
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 very clumsy at the moment) : import string
1
353
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 = '' def main():
0
7552
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7841
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8066
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7920
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6191
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1169
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.