Use apply() or not, and raising multiple exceptions (co-routines?)

Kragen Sitaker kragen at pobox.com
Tue Mar 19 03:24:29 EST 2002


Roy Smith <roy at panix.com> writes:
> The only thing I can see is to have each check method return a list of 
> problems it found.  Then, the main loop would look like:
> 
> for check in self.checks:
>    for problem in check (self, data):
>       print 'failed check:', problem
> 
> but that implies pushing a lot of scaffolding down into the check methods, 
> making them build and manipulate error lists to return.  I'm trying to keep 
> them as clean as possible, so they're easy to write and debug.

Well, PyUnit has the same problem.  How about this?  This skeleton is
about a dozen lines of code.

class basecheck:
    def __init__(self, complain): self.complain = complain

class somecheck(basecheck):
    def test(self, data):
        if isbad(data): self.complain("data is bad")

class complainer:
    def __init__(self): self.complaints = []
    def __call__(self, complaint): self.complaints.append(complaint)

def runsomechecks(checks, data):
    acomplainer = complainer()
    for check in checks: check(acomplainer).test(data)
    for problem in acomplainer.complaints: print "failed check:', problem

But maybe you should just use PyUnit, which is in the standard distribution.




More information about the Python-list mailing list