<br><br><div class="gmail_quote">On Mon, Sep 27, 2010 at 11:43 AM, Brian Jones <span dir="ltr">&lt;<a href="mailto:bkjones@gmail.com">bkjones@gmail.com</a>&gt;</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">&lt;<a href="mailto:tim@lashni.net" target="_blank">tim@lashni.net</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


I&#39;ve got a small function that I&#39;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 &quot;if re.search&quot; for the patterns but it looked terrible so I&#39;ve read up on list comprehensions and it&#39;s slightly improved. I just can&#39;t escape the feeling that there&#39;s a more elegant way to do this that I&#39;m missing.<br>



<br>
I&#39;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&#39;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>
    &quot;&quot;&quot;Checks password for sufficient complexity.&quot;&quot;&quot;<br>
    if len(password) &lt; 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 = &#39;asdf1234A&#39;</div><div>

for c in d:</div><div>   if not [x for x in s if x in c]:</div><div>       print x, &#39; not in &#39;, 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 &#39;len&#39; and &#39;== 0&#39;: </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 &#39;return [c for c in password if c in ascii_lowercase]&#39; 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>