<br><br><div class="gmail_quote">On Mon, Sep 27, 2010 at 11:43 AM, Brian Jones <span dir="ltr"><<a href="mailto:bkjones@gmail.com">bkjones@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br><br><div class="gmail_quote"><div class="im">On Mon, Sep 27, 2010 at 11:09 AM, Tim Miller <span dir="ltr"><<a href="mailto:tim@lashni.net" target="_blank">tim@lashni.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I've got a small function that I'm using to check whether a password is of a certain length and contains mixed case, numbers and punctuation.<br>
<br>
Originally I was using multiple "if re.search" for the patterns but it looked terrible so I've read up on list comprehensions and it's slightly improved. I just can't escape the feeling that there's a more elegant way to do this that I'm missing.<br>
<br>
I've been looking through all the python stuff that I thought might be relevant (lambda, map, filter, set, frozenset, etc) but nothing has come together. Just wondering if anyone has suggested reading material for alternate ways they'd handle this code.<br>
<br>
CODE:<br>
<br>
from string import ascii_lowercase, ascii_uppercase, digits, punctuation<br>
<br>
<br>
def complex_password(password):<br>
"""Checks password for sufficient complexity."""<br>
if len(password) < 12:<br>
return False<br>
if len([c for c in password if c in punctuation]) == 0:<br>
return False<br>
if len([c for c in password if c in digits]) == 0:<br>
return False<br>
if len([c for c in password if c in ascii_uppercase]) == 0:<br>
return False<br>
if len([c for c in password if c in ascii_lowercase]) == 0:<br>
return False<br>
return True<br></blockquote></div></div></blockquote><div><br></div><div>How about this: </div><div><br></div><div>d = [digits, punctuation, ascii_uppercase, ascii_lowercase]</div><div>s = 'asdf1234A'</div><div>
for c in d:</div><div> if not [x for x in s if x in c]:</div><div> print x, ' not in ', c</div><div><br></div><div>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. </div>
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="gmail_quote"><div class="im"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote><div><br></div><div><br></div></div><div>You can probably make other optimizations, but just to start, you can get rid of 'len' and '== 0': </div><div><br></div><div>if not [c for c in password if c in ascii_lowercase]:</div>
<div> return False</div><div><br></div><div>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. </div>
<div class="im">
<div><br></div><div> </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</blockquote></div></div><font color="#888888"><br><br clear="all"><br>-- <br>Brian K. Jones<br>My Blog <a href="http://www.protocolostomy.com" target="_blank">http://www.protocolostomy.com</a><br>Follow me <a href="http://twitter.com/bkjones" target="_blank">http://twitter.com/bkjones</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Brian K. Jones<br>My Blog <a href="http://www.protocolostomy.com">http://www.protocolostomy.com</a><br>Follow me <a href="http://twitter.com/bkjones">http://twitter.com/bkjones</a><br>