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

Conversation

qckzr
Copy link
Contributor

@qckzr qckzr commented Jul 7, 2019

  • Added main() function
  • Simplified password_generator
  • Formatted file according to pep 8

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

Very cool... Thanks!

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.

@cclauss cclauss merged commit 234b0a7 into TheAlgorithms:master Jul 7, 2019
@TheAlgorithms TheAlgorithms deleted a comment Jul 7, 2019
@qckzr qckzr deleted the password-generator branch July 14, 2019 02:59
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* Added print function into matrix_multiplication_addition.py and removed blank space in data_structures/binary tree directory

* Removed .vs/ folder per TheAlgorithms#893

* Rename matrix_multiplication_addition.py to matrix_operation.py

* Added main() function and simplified password generation.

* Modified password_generator.py file according to suggestions in TheAlgorithms#968
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants