Skip to content

Instantly share code, notes, and snippets.

@nifo
Last active December 27, 2015 10:59
Show Gist options
  • Save nifo/7315681 to your computer and use it in GitHub Desktop.
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…
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