osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can one make 'in' ungreedy?


>
> Thanks for taking the trouble to look. It's a *bit* difficult to run
> in the debugger as the program is a filter triggered by incoming
> E-Mail messages.  However I think I can fire stuff at it via stdin so
> I'll see what I can fathon out doing that.
>

Cheapo debug trick: When your filter starts, have it open a file in /tmp
(or similar) with "a" mode, leaving that file object as a global variable.
Then at points of interest, print out useful values to that open file
object. Once you've got your logic sorted, either remove the code, comment
it out, or predicate its action based on some debug flag. Briefly, using
Larry's example:

    DBG_FP = open("/tmp/ipswich", "a")

You might also want to print the message's message-id to DBG_FP at this
point. It will serve as a useful separator between debug output for
different messages.

Sometime later:

    print("subject:", subject, file=DBG_FP)
    print("ipswich?", ipswich in subject, file=DBG_FP)

And so on. If there is some conditional nesting/looping going on, it can be
useful to also include a unique number in each print call in case it's not
immediately apparent where a particular record in your file came from.

    print(1, "subject:", subject, file=DBG_FP)
    print(2, "ipswich?", ipswich in subject, file=DBG_FP)

I started using Python long before there were any fancy debuggers available
(never cottoned to pdb for some reason), so print statements were a very
common way to debug logic errors. I must confess to continuing to rely on
them instead of figuring out how to use modern IDEs (I'm still an Emacs guy
anyway, so IDEs seem to suck as a way to edit code).

Skip