Skip to content

Commit fc178b0

Browse files
authored
Update password.py
Add new feature about the password generator function, and make code style more standardized.
1 parent 37c6353 commit fc178b0

File tree

1 file changed

+56
-11
lines changed

1 file changed

+56
-11
lines changed
Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,74 @@
11
import string
22
import secrets
33

4-
# Define the set of characters to be used in the password
5-
CHARACTER_SET = string.ascii_letters + string.digits + string.punctuation
4+
from enum import Enum
65

7-
def generate_password(length):
8-
"""Generate a random password of the specified length."""
9-
password = ''.join(secrets.choice(CHARACTER_SET) for i in range(length))
6+
7+
# Define the set of characters to be used in the password, three strings means three modes.
8+
9+
10+
class CharcterSet(Enum):
11+
ONLYNUMBER = string.digits
12+
NOPUNCTUATION = string.ascii_letters + string.digits
13+
ALLCHARCTER = string.ascii_letters + string.digits + string.punctuation
14+
15+
16+
# Define a list of CharcterSet , in order to quickly transfer mode code to corresponding character set.
17+
18+
CharcterSetList = [
19+
CharcterSet.ALLCHARCTER,
20+
CharcterSet.ONLYNUMBER,
21+
CharcterSet.NOPUNCTUATION,
22+
]
23+
24+
# Define Prompt Enum class , in order to amend prompts more easily .
25+
26+
27+
class Prompt(Enum):
28+
GetPasswordInt = "How many passwords do you want to generate? "
29+
GetPasswordLength = "Enter the length of the password(s): "
30+
GetGenerationMode = "Choose password generation mode: (1) Only Number (2) Don't use punctuation (0) All ascii character "
31+
ValueErrorOccured = "Please enter a valid integer."
32+
PasswordGenerated = "Generated passwords:"
33+
34+
35+
def generate_password(length: int, generation_mode_code: int = 0) -> str:
36+
"""
37+
generate_password
38+
39+
Generate a random password of the specified length by the specified mode.
40+
41+
Args:
42+
length (int): specify the length of password.
43+
generation_mode_code (int): specify the generation mode of password: (1) Only Number (2) Don't use punctuation (0/default) All ascii character.
44+
45+
Returns:
46+
str: a password string
47+
"""
48+
password = "".join(
49+
secrets.choice(CharcterSetList[generation_mode_code].value)
50+
for i in range(length)
51+
)
1052
return password
1153

54+
1255
def main():
1356
# Prompt the user for the number of passwords to generate and their length
1457
while True:
1558
try:
16-
num_pass = int(input("How many passwords do you want to generate? "))
17-
password_length = int(input("Enter the length of the password(s): "))
59+
num_pass = int(input(Prompt.GetPasswordInt.value))
60+
password_length = int(input(Prompt.GetPasswordLength.value))
61+
generation_mode_code = int(input(Prompt.GetGenerationMode.value))
1862
break
1963
except ValueError:
20-
print("Please enter a valid integer.")
21-
continue
64+
print(Prompt.ValueErrorOccured.value)
65+
continue
2266
# Generate the specified number of passwords and print them to the console
23-
print("Generated passwords:")
67+
print(Prompt.PasswordGenerated.value)
2468
for i in range(num_pass):
25-
password = generate_password(password_length)
69+
password = generate_password(password_length, generation_mode_code)
2670
print(f"{i+1}. {password}")
2771

72+
2873
if __name__ == "__main__":
2974
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy