How to use "OnlyOnce"

2009-08-30
2013-05-14
  • I would like that the following action is only done/run once per file I am parsing.

    Tried:
            # buyer
            str_buyer = pyp.oneOf("buyer:;" need to watch this, and maybe make onlyonce work
                                  "buyer information:;"
                                  "acheteur :".split(';'), caseless=True).suppress() +\                               alphanums + pyp.restOfLine
            buyer = pyp.OnlyOnce(str_buyer.setParseAction(self.str_buyerAction))

    But the parse action is still run twice, once for "buyer information" and once for "buyer".

    What am I doing wrong?

    Werner

     
    • My work around is to just check a variable in "self.str_buyerAction", that works fine for me.

      But I am still intrigued on how one would use OnlyOnce.

      Werner

       
    • Paul McGuire
      Paul McGuire
      2009-09-07

      OnlyOnce is really a modifier on the parse action, not on the expression.  Try:

      buyer = str_buyer.setParseAction(pyp.OnlyOnce(self.str_buyerAction))

      -- Paul

       
    • Aha, that is how that is used.

      Great, it is much cleaner/self documenting then setting a flag in str_buyerAction (which was my work around).

      Thanks again
      Werner