Pyparsing SetParseAction 问题
我是 pyparsing 的新手,我正在尝试使用 setParseAction 但有时不会被调用。 代码。
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')
这是我不明白为什么第一次不调用 setParseAction 的
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚第一次使用 pyparsing,所以......
在初始化类变量 comdty_tok 时,您从未真正调用过 parseString() ,因此与解析对象关联的回调从未被调用过。
I just played with pyparsing for the first time, so ...
In initializing the class variable
comdty_tok
you never actually callparseString()
, therefore the callback associated with the parse object is never called.我只能说,我并不是真的打算像您所做的那样扩展像 Token 这样的类。我怀疑在您对包含的
cmdty_tok
属性的委托中,您忽略了公开一些其他属性,例如parseAction
,该属性通常会在解析时由引用parseImpl。另一方面,您的 fun() 实现与我使用过和见过的其他帮助器和闭包非常一致,毫不奇怪,这种方法有效。
您想通过
ComdtyTok
实现什么目标?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 asparseAction
, which would normally be referenced at parse time byparseImpl
. On the other hand, your implementation offun()
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
?