A very useful debugging trick

2006-04-04
2013-05-14
  • I was constantly annoyed at having to use setName in order to get useful debugging messages so I went looking for a way to automatically setName for all of my ParserExpressions and I came up with this very useful little 3-line trick.  I turn it on while I'm trying to figure out what pyparsing is doing and comment it out or delete it after everything is working well.  Note that you'll need to locate this code right before your call to parseString or scanString because the ParseExpressions need to already be defined for it to work.

        for pename, peobject in locals().iteritems():
            if isinstance(peobject, ParseExpression):
                peobject = peobject.setName(pename).setDebug()

    Note that you could also use this trick to setResultsName but that usually isn't how I want my parsers to behave.

     
    • Paul McGuire
      Paul McGuire
      2006-04-05

      Very nice.  I've been meaning to pull together some general debugging tips, I'll be sure to include this technique.

      -- Paul