Re: Pyparsing help



On Mar 22, 6:30 pm, Paul McGuire <pt...@xxxxxxxxxxxxx> wrote:
Oof, I see that you have multiple "Layer" entries, with different
qualifying labels.  Since the dicts use "Layer" as the key, you only
get the last "Layer" value, with qualifier "PRBOUNDARY", and lose the
"Layer" for "METAL2".  To fix this, you'll have to move the optional
alias term to the key, and merge "Layer" and "PRBOUNDARY" into a
single key, perhaps "Layer/PRBOUNDARY" or "Layer(PRBOUNDARY)" - a
parse action should take care of this for you.  Unfortnately, these
forms will not allow you to use object attribute form
(md.Layer.lineStyle), you will have to use dict access form
(md["Layer(PRBOUNDARY)"].lineStyle), since these keys have characters
that are not valid attribute name characters.

Or you could add one more level of Dict nesting to your grammar, to
permit access like "md.Layer.PRBOUNDARY.lineStyle".

-- Paul

OK - We'll I got as far as you did but I did it a bit differently..
Then I merged some of your data with my data. But Now I am at the
point of adding another level of the dict and am struggling.. Here is
what I have..

# parse actions
LPAR = Literal("(")
RPAR = Literal(")")
LBRACE = Literal("{")
RBRACE = Literal("}")
EQUAL = Literal("=")

# This will get the values all figured out..
# "metal2" 1 6.05E-05 30
cvtInt = lambda toks: int(toks[0])
cvtReal = lambda toks: float(toks[0])

integer = Combine(Optional(oneOf("+ -")) + Word(nums))\
.setParseAction( cvtInt )
real = Combine(Optional(oneOf("+ -")) + Word(nums) + "." +
Optional(Word(nums)) +
Optional(oneOf("e E")+Optional(oneOf("+ -"))
+Word(nums)))\
.setParseAction( cvtReal )
atfstr = quotedString.setParseAction(removeQuotes)
atflist = Group( LPAR.suppress() +
delimitedList(real, ",") +
RPAR.suppress() )

atfvalues = ( real | integer | atfstr | atflist )

# Now this should work out a single line inside a section
# maskName = "metal2"
# isDefaultLayer = 1
# visible = 1
# fatTblSpacing = (0.21,0.24,0.6,
# 0.6,0.6,0.6)
# minArea = 0.144
atfkeys = Word(alphanums)
attrDict = dictOf( atfkeys , EQUAL.suppress() + atfvalues)

# Now we need to take care of the "Metal2" { one or more
attrDict }
# "METAL2" {
# layerNumber = 36
# maskName = "metal2"
# isDefaultLayer = 1
# visible = 1
# fatTblSpacing =
(0.21,0.24,0.6,
#
0.24,0.24,0.6,
#
0.6,0.6,0.6)
# minArea = 0.144
# }
attrType = dictOf(atfstr, LBRACE.suppress() + attrDict +
RBRACE.suppress())

# Lastly we need to get the ones without attributes (Technology)
attrType2 = LBRACE.suppress() + attrDict + RBRACE.suppress()
mainDict = dictOf(atfkeys, attrType2 | attrType )

md = mainDict.parseString(test1)


But I too am only getting the last layer. I thought if broke out the
"alias" area and then built on that I'd be set but I did something
wrong.

.



Relevant Pages

  • Re: Ordering python sets
    ... of Python is that the built-in data structures (list, dict, set) are ... using binary tree is a O. ... I don't care how print statement works, unless I'm trying to do something ... have all the features and functionality as earth dining plate, ...
    (comp.lang.python)
  • Re: Pyparsing help
    ... Oof, I see that you have multiple "Layer" entries, with different ... parse action should take care of this for you. ... you will have to use dict access form ... Or you could add one more level of Dict nesting to your grammar, ...
    (comp.lang.python)
  • Re: simultaneous assignment
    ... Wouldn't reducing to a set instead of a dict make more sense if all you want ... My apologies for not explaining tallybetter. ... If you didn't care how many times the values appeared in the original ...
    (comp.lang.python)
  • Re: Constants for catch return values?
    ... Why do we care about ... thickheads using "-token" as a command name, ... as a command name. ... If your script defines a "dict" proc, ...
    (comp.lang.tcl)
  • Re: What is class method?
    ... call it from any dictionary, but it doesn't care which dict you call ... If 'fromkeys' were a staticmethod, it would have to be hardcoded to ...
    (comp.lang.python)