comparing all values of a list to regex
aleax at aleax.it
Fri Sep 27 12:57:37 CEST 2002
Manuel Hendel wrote:
> Sorry for forgetting some details.
> On Fri, Sep 27, 2002 at 08:07:24AM +0000, Alex Martelli wrote:
>> def classify(login_email):
>> l_e = login_email.split(',')
> I think that I nearly understand what you are doing here. But I don't
> understand the assert part. I already checked at the documentations
> but this doesn't helped me at all.
>> assert 1 <= len(l_e) <= 2, "More than one comma in (%s)" %
assert is just a sanity check. I want the program to bail out
with a clear error message if it's ever presented with an input
file that does NOT respect the specs you've given (the only
ones which the program is prepared to handle), rather than
keep chugging and producing meaningless, misleading results.
assert is nice to use for this purpose, because when you run
the program with python -O it silently goes away and you pay
no performance price whatsoever for having assert there.
By the same token, assert may not be the right statement in
this case, because I'm validating sanity of *input*, NOT
sanity of my program. Even when the program is fully valid
it may still be presented with invalid input, and it needs
to scream when that happens.
So, you may prefer to change the assert into, e.g.:
L = len(l_e)
if L<1 or L>2:
raise ValueError, "%s commas in (%r)" % (L-1, login_email)
More information about the Python-list