[Tutor] function with multiple checks

Tim Miller tim at lashni.net
Mon Sep 27 19:06:13 CEST 2010


On 28/09/10 01:50, Brian Jones wrote:
>
>
> On Mon, Sep 27, 2010 at 11:43 AM, Brian Jones <bkjones at gmail.com
> <mailto:bkjones at gmail.com>> wrote:
>
> How about this:
>
> d = [digits, punctuation, ascii_uppercase, ascii_lowercase]
> s = 'asdf1234A'
> for c in d:
>     if not [x for x in s if x in c]:
>         print x, ' not in ', c
>
> Just a quick hack, but you get the idea. It breaks when you want
> different numbers of characters from the different lists in the password.
>
>     You can probably make other optimizations, but just to start, you
>     can get rid of 'len' and '== 0':
>
>     if not [c for c in password if c in ascii_lowercase]:
>         return False
>
>     will return False if there are no lowercase characters. An empty
>     list evaluates to False. With that in mind, you could just 'return
>     [c for c in password if c in ascii_lowercase]' if the calling code
>     is written to handle it.

I've rewritten that section of code using the lambda example from this 
thread since to me it just looks very clean. Thanks for the pointers on 
list comprehensions though, I've managed to go through and simplify 
quite a few bits of code based on that. I'm a little embarrased about 
all the len() now. :)


More information about the Tutor mailing list