Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > More fun with PyParsing - almost did it on my own..

Reply
Thread Tools

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

 
 
rh0dium
Guest
Posts: n/a
 
      05-14-2008
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 """
 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      05-15-2008
On May 14, 6:07*pm, rh0dium <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
3 PIX VPN questions - FUN FUN FUN frishack@gmail.com Cisco 3 03-16-2006 02:25 PM
OT: Wednesday follow-up-to-Tuesday-Fun Fun Ken Briscoe MCSE 0 07-14-2004 01:41 PM
Programming is not as much fun/more fun than it used to be. Andy Fish Java 65 05-18-2004 08:24 PM
Fun fun fun Luke Computer Support 3 10-07-2003 03:45 PM



Advertisments