473,394 Members | 1,813 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,394 software developers and data experts.

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" "PinDirection"
"OptionalSignal" )
guts = Group( LPAR.suppress() +
quotedString.setParseAction(removeQuotes).setResul tsName("name") +
quotedString.setParseAction(removeQuotes).setResul tsName("direction")
+
Optional(quotedString.setParseAction(removeQuotes) .setResultsName("signal"))
+ RPAR.suppress())

# This will simply wrap the Cell Info around it
cell = Group( Literal("dbSetCellPortTypes").suppress() +
quotedString.setParseAction(removeQuotes).setResul tsName("library") +
quotedString.setParseAction(removeQuotes).setResul tsName("name") +
Literal("'").suppress() + LPAR.suppress() +
OneOrMore(guts).setResultsName("pins") + RPAR.suppress() ) +
Literal("#f").suppress() | Literal("#t").suppress()

# This grabs many cells
cells = OneOrMore(cell)

OK and it sorta works if I do the following:

x = cells.parseString(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':'stdcell130'
'pins': [ { 'name': 'VSS', 'direction':'Inout', 'signal':'Ground'},
{ 'name': 'VDD', 'direction':'Inout', 'signal':'Power'},
{ 'name': 'A', 'direction':'Input' },
{ 'name': 'B', 'direction':'Input' },
{ 'name': 'Y', 'direction':'Output' } ]
}

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 = """dbSetCellPortTypes "stdcell130" "AND2X1" '(
("A" "Input" )
("B" "Input" )
("Y" "Output" )
("VDD" "Inout" "Power" )
("VSS" "Inout" "Ground" )
) #f
dbSetCellPortTypes "stdcell130" "AND2X2" '(
("A" "Input" )
("B" "Input" )
("Y" "Output" )
("VDD" "Inout" "Power" )
("VSS" "Inout" "Ground" )
) #f """
Jun 27 '08 #1
1 1665
On May 14, 6:07*pm, rh0dium <steven.kl...@gmail.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.parseString(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':'stdcell130'
* 'pins': [ { 'name': 'VSS', 'direction':'Inout', 'signal':'Ground'},
* * * * * * *{ 'name': 'VDD', 'direction':'Inout', 'signal':'Power'},
* * * * * * *{ 'name': 'A', 'direction':'Input' },
* * * * * * *{ 'name': 'B', 'direction':'Input' },
* * * * * * *{ 'name': 'Y', 'direction':'Output' } ]

}

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)).setResultsName("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(quotedString.setParseAction(removeQuotes) .setResultsName("signal"))

to:

+
Optional(quotedString.setParseAction(removeQuotes) ,default="").setResultsName("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.signal
print cell.pins.A.direction
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
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...
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
1
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)...
3
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...
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 := "+"...
3
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...
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...
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 = ''...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.