-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Conversation
qckzr
commented
Jul 7, 2019
- Added main() function
- Simplified password_generator
- Formatted file according to pep 8
…ed blank space in data_structures/binary tree directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool... Thanks!
other/password_generator.py
Outdated
random.shuffle(chars) | ||
password = ''.join(random.choice(chars) | ||
for x in range(max_length)) | ||
return password |
There was a problem hiding this comment.
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.
* 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