Skip to content

Instantly share code, notes, and snippets.

@mka142
Created April 5, 2022 15:03
Show Gist options
  • Save mka142/553267243161da338456990e1a9ec69c to your computer and use it in GitHub Desktop.
Save mka142/553267243161da338456990e1a9ec69c to your computer and use it in GitHub Desktop.
Generate password in python using letters, numbers and punctation
import random
def password_generator(length=50,table=[1,1,0]): # to refactor
IS_CHOOSEEN = lambda x,y: x if y==1 else ''
LETTERS = IS_CHOOSEEN(string.ascii_letters,table[0])
NUMBERS = IS_CHOOSEEN(string.digits,table[1])
PUNCTUATION = IS_CHOOSEEN(string.punctuation,table[2])
# create alphanumerical by fetching string constant
printable = NUMBERS + LETTERS + PUNCTUATION
# convert printable from string to list and shuffle
printable = list(printable)
random.shuffle(printable)
# generate random password
random_password = random.choices(printable, k=length)
# convert generated password to string
random_password = ''.join(random_password)
return random_password
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment