Last active
December 27, 2015 10:59
-
-
Save nifo/7315681 to your computer and use it in GitHub Desktop.
A couple of methods used for building a neat password. passwords is a generator of random passwords using a specfic set of characters and a min and max length. valid_password is just one validation, you can certainly make up more requirements for what should pass as a valid password. This one is used to specify how many digits, lowercase and upp…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def password_generator(min_len= 10, max_len=18, | |
chars='abcdefghjkmnpqrstauvz123456789ABCDEFGHIJKLMNOPQRSTUVXYZ!#$@'): | |
"Generator of random passwords using the character set 'chars'" | |
from random import randint | |
length = randint(min_len,max_len) | |
while True: | |
yield ''.join(choice(chars) for i in range(length)) | |
def valid_password(password, digits=0, lower_case=0, upper_case=0): | |
"""Returns true if there's more than 2 digits, more than 4 lowercase | |
and more than 4 uppcase letters in a password""" | |
count = lambda chars: sum(1 for char in password if char in chars) | |
return (count('0123456789') >= digits and | |
count('abcdefghijklmnopqrstuvwxyz') >= lower_case and | |
count('ABCDEFGHIJKLMNOPQRSTUVwXYZ') >= upper_case) | |
def generate_password(): | |
"Returns a valid password with at least 2 digits, 2 upper and 2 lower case chars." | |
return next(pw for pw in password_generator() if valid_password(pw, 2, 2, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment