Skip to content

Instantly share code, notes, and snippets.

@ozkansen
Created October 27, 2020 13:55
Show Gist options
  • Save ozkansen/9ee9eedcd5c96e0bf85af4457856ed4e to your computer and use it in GitHub Desktop.
Save ozkansen/9ee9eedcd5c96e0bf85af4457856ed4e to your computer and use it in GitHub Desktop.
import random
import string
class RandomPassword:
def __init__(self, length=8, string=True, number=True, special=False) -> None:
self.length = length
self.string = string
self.number = number
self.special = special
def collect(self):
collect_char = ""
if self.string:
collect_char += string.ascii_letters
if self.number:
collect_char += string.digits
if self.special:
collect_char += string.punctuation
return collect_char
def generate(self) -> str:
password = "".join(
random.sample(self.collect(), self.length)
)
return password
if __name__ == '__main__':
r = RandomPassword(length=16)
for i in range(5):
gen = r.generate()
print(f"Create Password {i+1}: \"{gen}\"")
# Output :
# Create Password 1: "2ysnrfTqcR1hEOgm"
# Create Password 2: "X8Elhq4FcIJS0Tm9"
# Create Password 3: "bZH3260ya48NnwXf"
# Create Password 4: "EyaZzNuJlAwext73"
# Create Password 5: "RACSgQUPmWqY0i8l"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment