Skip to content

Commit 54051ad

Browse files
First version
1 parent 3c7a8b7 commit 54051ad

File tree

6 files changed

+169
-0
lines changed

6 files changed

+169
-0
lines changed

__pycache__/generator.cpython-310.pyc

1.29 KB
Binary file not shown.

__pycache__/utils.cpython-310.pyc

656 Bytes
Binary file not shown.

generator.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
generator.py - Generates the passwords.
3+
"""
4+
5+
# Imports
6+
from random import choice # To generate random letters
7+
import json # To import the letter groups
8+
9+
# Load letter groups
10+
with open("letter-groups.json", mode="r") as f:
11+
letter_groups = json.load(f)
12+
13+
def get_random_letter(letters):
14+
"""
15+
Gets a random letter out of letters.
16+
"""
17+
18+
return choice(letters)
19+
20+
def get_letters(letter_group_settings):
21+
"""
22+
Gets all letters from a option set of letter groups.
23+
24+
"""
25+
26+
return "".join([letter_groups[i] for i in letter_group_settings if i in letter_groups])
27+
28+
def generate_password(letter_groups, length):
29+
"""
30+
Generates a password of length `length` and the letter groups `letter_groups`.
31+
"""
32+
33+
letters = get_letters(letter_groups)
34+
35+
password = "".join([get_random_letter(letters) for _ in range(length)])
36+
37+
return password

letter-groups.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"lowercase": "qwertyuiopasdfghjklzxcvbnm",
3+
"uppercase": "QWERTYUIOPASDFGHJKLZXCVBNM",
4+
"numbers": "1234567890",
5+
"symbols": "!@#$(){}[];:+=-_"
6+
}

main.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
The user interface.
3+
"""
4+
5+
# Imports
6+
from generator import generate_password, letter_groups # To generate the password
7+
from utils import get_yes_or_no, spilt_number_into_two_halves
8+
from threading import Thread
9+
10+
def get_user_options():
11+
"""
12+
Gets the options the user wants for a password.
13+
"""
14+
15+
letter_group_options = []
16+
for letter_group_name in letter_groups.keys():
17+
if get_yes_or_no(f"Do {letter_group_name}?"):
18+
letter_group_options.append(letter_group_name)
19+
20+
while True:
21+
length = input("Length? ")
22+
23+
if not length.isdigit():
24+
print("Please enter a number.")
25+
continue
26+
27+
length = int(length)
28+
break
29+
30+
return {
31+
"letter_groups": letter_group_options,
32+
"length": length
33+
}
34+
35+
def main():
36+
"""
37+
The user interface.
38+
"""
39+
40+
print("Welcome to Password Generator!")
41+
print()
42+
while True:
43+
print("Options:")
44+
print("1. Generate a password")
45+
print("2. Generate multiple.")
46+
print("3. Exit")
47+
48+
option = input("What would you like to do?")
49+
50+
if not option.isdigit():
51+
print("Please enter a number.")
52+
continue
53+
54+
option = int(option)
55+
56+
if not 0 < option < 4:
57+
print("Please enter a number between 1 and 3.")
58+
continue
59+
60+
if option == 1:
61+
options = get_user_options()
62+
63+
print("Your password:", generate_password(options["letter_groups"], options["length"]))
64+
elif option == 2:
65+
options = get_user_options()
66+
67+
while True:
68+
amount_of_passwords = input("Amount? ")
69+
70+
if not amount_of_passwords.isdigit():
71+
print("Please enter a number.")
72+
continue
73+
74+
amount_of_passwords = int(amount_of_passwords)
75+
break
76+
77+
passwords = [generate_password(options["letter_groups"], options["length"]) for _ in range(amount_of_passwords)]
78+
79+
print(f"Your passwords: ")
80+
81+
for password in passwords:
82+
print(password)
83+
elif option == 3:
84+
print("Bye!")
85+
break
86+
87+
88+
if __name__=="__main__":
89+
main()

utils.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Utils.
3+
"""
4+
5+
YES = [
6+
"y",
7+
"yes",
8+
"yas",
9+
"yea",
10+
"yeah",
11+
"fax",
12+
"fax no printer",
13+
"true",
14+
"period",
15+
"1",
16+
"on",
17+
"true"
18+
]
19+
20+
def get_yes_or_no(prompt):
21+
"""
22+
Gets a yes or a no from the user.
23+
"""
24+
25+
user_input = input(prompt)
26+
27+
return user_input in YES
28+
29+
def spilt_number_into_two_halves(number):
30+
"""
31+
Split a number into two halves.
32+
"""
33+
34+
first_half = number // 2
35+
second_half = number - first_half
36+
37+
return first_half, second_half

0 commit comments

Comments
 (0)