How to use "OnlyOnce"

  • Werner F. Bruhin

    I would like that the following action is only done/run once per file I am parsing.

            # 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 F. Bruhin

      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.


    • 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

    • Werner F. Bruhin

      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


Log in to post a comment.