[Baypiggies] Discussion for newbies/beginner night talks

Aahz aahz at pythoncraft.com
Wed Feb 14 05:00:13 CET 2007

On Tue, Feb 13, 2007, Shannon -jj Behrens wrote:
> #) Using try/except for flow control:
> """Try to actually log the user in.
> BASIC IDEA: A million things can go wrong, and there are several ways to
> succeed.  You want to check for the ways that can succeed and continue on as
> soon as one of them does.  You want the non-local flow of control that
> exceptions provide, but you need it for both success *and* failure.
> """
> def doLoginAction(self):
>     """Try to actually log the user in."""
>     class PasswordAccepted(Exception): pass
>     try:
>         if check_password():        # This may raise KeyError.
>             raise PasswordAccepted
>         do_more_expensive_work()
>         and_even_more_expensive_work()
>         if check_other_password():  #  This may raise KeyError.
>             raise PasswordAccepted
>         raise KeyError
>     except KeyError:
>         self.setError("Invalid username or password.")
>         return
>     except PasswordAccepted:
>         pass
>     continue_successfully()

This really should not call setError(), it should simply raise
ValueError (ValueError is for invalid data).  Here's a more-complicated
working example from my TextDB.py module illustrating multiple levels of
control flow managed with exceptions:

class Excluded(StandardError):

class Found(StandardError):

class NotFound(StandardError):

def _fuzzy_match(string, filter):
    return string.find(filter) >= 0

def _exact_match(string, filter):
    return string == filter

# Includes by default are ANDed; excludes are always ORed
# Use "|" to OR includes; "!" indicates exclude
def filter(db, FilterString=None, fields=None, FuzzyMatch=True):
    includes, excludes = _buildFilters(FilterString)
    if fields is not None:
        fields = re_comma_field.split(fields)
        for i in range(len(fields)):
            fields[i] = fields[i].lower()
    if FuzzyMatch:
        match = _fuzzy_match
        match = _exact_match
    result = []
    for record in db:
        if fields is None:
            curr_fields = record.keys()
            curr_fields = []
            for field in fields:
                if field in record:
            for field in curr_fields:
                for item in record[field]:
                    item = item.lower()
                    for filter in excludes:
                        if match(item, filter):
                            raise Excluded
        except Excluded:
            for ORlist in includes:
                for filter in ORlist:
                        for field in curr_fields:
                            for item in record[field]:
                                if match(item, filter):
                                    raise Found
                    except Found:
                    raise NotFound
        except NotFound:
    return result
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"I disrespectfully agree."  --SJM

More information about the Baypiggies mailing list