Google Groups Home
Help | Sign in
More fun with PyParsing - almost did it on my own..
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
rh0dium  
View profile
 More options May 14, 7:07 pm
Newsgroups: comp.lang.python
From: rh0dium <steven.kl...@gmail.com>
Date: Wed, 14 May 2008 16:07:30 -0700 (PDT)
Local: Wed, May 14 2008 7:07 pm
Subject: 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).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 """


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul McGuire  
View profile
 More options May 14, 11:47 pm
Newsgroups: comp.lang.python
From: Paul McGuire <pt...@austin.rr.com>
Date: Wed, 14 May 2008 20:47:23 -0700 (PDT)
Local: Wed, May 14 2008 11:47 pm
Subject: Re: More fun with PyParsing - almost did it on my own..
On May 14, 6:07 pm, rh0dium <steven.kl...@gmail.com> wrote:

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="").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.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 to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google