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

R

rh0dium

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).setResultsName("name") +
quotedString.setParseAction(removeQuotes).setResultsName("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).setResultsName("library") +
quotedString.setParseAction(removeQuotes).setResultsName("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 """
 
P

Paul McGuire

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top