Skip to content

Instantly share code, notes, and snippets.

@ichux
Created March 11, 2021 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichux/866a0bfd2568c17cf507be58ae1dcfe2 to your computer and use it in GitHub Desktop.
Save ichux/866a0bfd2568c17cf507be58ae1dcfe2 to your computer and use it in GitHub Desktop.
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