Created
March 11, 2021 10:35
-
-
Save ichux/866a0bfd2568c17cf507be58ae1dcfe2 to your computer and use it in GitHub Desktop.
This file contains 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
import secrets | |
import keyword as _keyword | |
from difflib import get_close_matches | |
ALLOWED_CHARS = ( | |
'abcdefghijklmnopqrstuvwxyz' | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' | |
) | |
def get_random_string(length): | |
""" | |
Return a securely generated random string. | |
The bit length of the returned value can be calculated with the formula: | |
log_2(len(allowed_chars)^length) | |
For example, with default `allowed_chars` (26+26+10), this gives: | |
* length: 12, bit length =~ 71 bits | |
* length: 22, bit length =~ 131 bits | |
""" | |
return ''.join(secrets.choice(ALLOWED_CHARS) for i in range(length)) | |
# possible_matches = get_close_matches(subcommand, commands) | |
if __name__ == '__main__': | |
possible_matches = get_close_matches("appel", ["ape", "apple", "peach", "puppy"]) | |
print(possible_matches) | |
print(get_close_matches("accept", _keyword.kwlist)) | |
print(get_random_string(12)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment