parsing string to a list of objects - help!

Donn Ingle donn.ingle at gmail.com
Mon Nov 5 05:51:59 EST 2007


Paul,
I quickly slapped your example into a py file for a first-glance test and
the first part works, but the second gives me the error below. Could I have
an old version of pyparser? I will dig into it anyway, just being lazy :)

code:
from pyparsing import *

frame = Literal("#")
tween = Word("-") # that is, it is a "word" composed of 1 or more -'s
copy = Literal("=")
blank = Literal("_")

animation = OneOrMore((frame + Optional(
                            (tween + FollowedBy(frame)) |
                            OneOrMore(copy | blank) ) ) )

test = "#---#--#===_#"

print animation.parseString(test)

class Prop(object):
    def __init__(self,tokens):
        pass

class Tween(object):
    def __init__(self,tokens):
        self.tween = tokens[0]
        self.tweenLength = len(self.tween)

class CopyPrevious(object):
    def __init__(self,tokens):
        pass

class Blank(object):
    def __init__(self,tokens):
        pass

frame.setParseAction(Prop)
tween.setParseAction(Tween)
copy.setParseAction(CopyPrevious)
blank.setParseAction(Blank)


animation.parseString(test)

result:
containers:$ python parser.py
['#', '---', '#', '--', '#', '=', '=', '=', '_', '#']
Traceback (most recent call last):
  File "parser.py", line 39, in ?
    animation.parseString(test)
  File "/usr/lib/python2.4/site-packages/pyparsing.py", line 620, in
parseString
    loc, tokens = self.parse( instring.expandtabs(), 0 )
  File "/usr/lib/python2.4/site-packages/pyparsing.py", line 562, in parse
    loc,tokens = self.parseImpl( instring, loc, doActions )
  File "/usr/lib/python2.4/site-packages/pyparsing.py", line 1768, in
parseImpl
    loc, tokens = self.expr.parse( instring, loc, doActions )
  File "/usr/lib/python2.4/site-packages/pyparsing.py", line 562, in parse
    loc,tokens = self.parseImpl( instring, loc, doActions )
  File "/usr/lib/python2.4/site-packages/pyparsing.py", line 1390, in
parseImpl
    loc, resultlist = self.exprs[0].parse( instring, loc, doActions )
  File "/usr/lib/python2.4/site-packages/pyparsing.py", line 588, in parse
    tokens = self.parseAction( instring, tokensStart, retTokens )
TypeError: __init__() takes exactly 2 arguments (4 given)





More information about the Python-list mailing list