473,703 Members | 2,669 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 1681
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
2255
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) How can I get pyparsing to match 'atodkj45k' and 'atod_asdfaw', but not 'atgdkasdjfhlksj' and...
4
2073
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
1859
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
1303
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. So I thought I'd look into the pyparsing module, but can't find a simple example of processing...
4
1588
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
2194
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
2059
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 was a nice, green 'download' button that stood out from the page. 4) I clicked on the download...
1
2645
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
9102
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8995
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8951
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7844
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5920
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4674
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.