Skip to content

Simplied password_generator.py #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 7, 2019
47 changes: 29 additions & 18 deletions other/password_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,45 @@
import string
import random

letters = [letter for letter in string.ascii_letters]
digits = [digit for digit in string.digits]
symbols = [symbol for symbol in string.punctuation]
chars = letters + digits + symbols
random.shuffle(chars)

min_length = 8
max_length = 16
password = ''.join(random.choice(chars) for x in range(random.randint(min_length, max_length)))
print('Password: ' + password)
print('[ If you are thinking of using this passsword, You better save it. ]')
def password_generator(max_length):
letters = [letter for letter in string.ascii_letters]
digits = [digit for digit in string.digits]
symbols = [symbol for symbol in string.punctuation]
chars = letters + digits + symbols
random.shuffle(chars)
password = ''.join(random.choice(chars)
for x in range(max_length))
return password
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really cool ideas here! You sparked my imagination over my morning coffee. Thanks.

I approve this PR in its current form but what about...

from random import choice
from string import ascii_letters, digits, punctuation

def password_generator(length=8):
    '''
    >>> len(password_generator())
    8
    >>> len(password_generator(length=16))
    16
    >>> len(password_generator(257))
    257
    >>> len(password_generator(length=0))
    0
    >>> len(password_generator(-1))
    0
    '''
    chars = tuple(ascii_letters) + tuple(digits) + tuple(punctuation)
    return ''.join(choice(chars) for x in range(length))

Some of the logic behind these suggestions:

  • Change max_length to just length because it completely determines the length of the output
  • Give length a sensible default value so that users can just fire-and-forget. Perhaps this value should be higher
  • Add doctests so that we can test the code by running python3 -m doctest -v other/password_generator.py
  • tuples should be faster to create then lists and should take up slightly less memory
  • We only need to randomize once. random.choice() should be a lighter weight operation than random.shuffle()
  • password_generator() should generate a password so there is no need to create a variable named password only to get rid of it on the following line.



# ALTERNATIVE METHODS
# ALTERNATIVE METHODS
# ctbi= characters that must be in password
# i= how many letters or characters the password length will be
def password_generator(ctbi, i):
# Password generator = full boot with random_number, random_letters, and random_character FUNCTIONS
pass # Put your code here...
# i= how many letters or characters the password length will be
def alternative_password_generator(ctbi, i):
# Password generator = full boot with random_number, random_letters, and
# random_character FUNCTIONS
pass # Put your code here...


def random_number(ctbi, i):
pass # Put your code here...
pass # Put your code here...


def random_letters(ctbi, i):
pass # Put your code here...
pass # Put your code here...


def random_characters(ctbi, i):
pass # Put your code here...
pass # Put your code here...


def main():
max_length = int(
input('Please indicate the max length of your password: '))
print ('Password generated:', password_generator(max_length))
print('[If you are thinking of using this passsword, You better save it.]')


if __name__ == '__main__':
main()