lesnar_56
lesnar_56

Reputation: 105

Pyparsing SetParseAction trouble

I am a new-bee to pyparsing I am trying to experiment with setParseAction but it is not being called sometimes. Here is the code

def fun():
    comdty_tok = StringStart() + Word(alphas) + StringEnd()
    comdty_tok.setParseAction(call_back)
    comdty_tok.leaveWhitespace()
    return comdty_tok

def call_back(p):
    print 'Calling ....'
    print p

class ComdtyTok(Token):

     comdty_tok = StringStart() + Word(alphas) + StringEnd()
     comdty_tok.setParseAction(call_back)
     comdty_tok.leaveWhitespace()
     parseImpl = comdty_tok.parseImpl

class SymParser(object):
    tok =  ComdtyTok()
    @staticmethod
    def parse(symbol):
       p = SymParser.tok.parseString(symbol)
       print p
       print "Second"
       x = fun()
       x.parseString(symbol)
       return p

SymParser.parse('ABCD')

I dont understand why the setParseAction is not called for the first time.

Upvotes: 1

Views: 453

Answers (2)

PaulMcG
PaulMcG

Reputation: 63739

All I can say is that I did not really intend for classes like Token to be extended in the manner that you have done. I suspect that in your delegation to the contained cmdty_tok attribute that you have omitted exposing some other attribute, such as parseAction, which would normally be referenced at parse time by parseImpl. On the other hand, your implementation of fun() is very consistent with other helpers and closures I have used and seen used, and not surprisingly, this approach works.

What are you trying to accomplish with ComdtyTok?

Upvotes: 1

Peter Rowell
Peter Rowell

Reputation: 17713

I just played with pyparsing for the first time, so ...

In initializing the class variable comdty_tok you never actually call parseString(), therefore the callback associated with the parse object is never called.

Upvotes: 2

Related Questions