From fc178b0a46e52721e374e345497c937f8b3dd103 Mon Sep 17 00:00:00 2001 From: Wu Ziban <2314245651@qq.com> Date: Sun, 30 Jun 2024 00:45:40 +0800 Subject: [PATCH 1/2] Update password.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add new feature about the password generator function, and make code style more standardized. --- PASSWORD RELATED/RandomPassword/password.py | 67 +++++++++++++++++---- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/PASSWORD RELATED/RandomPassword/password.py b/PASSWORD RELATED/RandomPassword/password.py index c3c58138..6fb6aa66 100644 --- a/PASSWORD RELATED/RandomPassword/password.py +++ b/PASSWORD RELATED/RandomPassword/password.py @@ -1,29 +1,74 @@ import string import secrets -# Define the set of characters to be used in the password -CHARACTER_SET = string.ascii_letters + string.digits + string.punctuation +from enum import Enum -def generate_password(length): - """Generate a random password of the specified length.""" - password = ''.join(secrets.choice(CHARACTER_SET) for i in range(length)) + +# Define the set of characters to be used in the password, three strings means three modes. + + +class CharcterSet(Enum): + ONLYNUMBER = string.digits + NOPUNCTUATION = string.ascii_letters + string.digits + ALLCHARCTER = string.ascii_letters + string.digits + string.punctuation + + +# Define a list of CharcterSet , in order to quickly transfer mode code to corresponding character set. + +CharcterSetList = [ + CharcterSet.ALLCHARCTER, + CharcterSet.ONLYNUMBER, + CharcterSet.NOPUNCTUATION, +] + +# Define Prompt Enum class , in order to amend prompts more easily . + + +class Prompt(Enum): + GetPasswordInt = "How many passwords do you want to generate? " + GetPasswordLength = "Enter the length of the password(s): " + GetGenerationMode = "Choose password generation mode: (1) Only Number (2) Don't use punctuation (0) All ascii character " + ValueErrorOccured = "Please enter a valid integer." + PasswordGenerated = "Generated passwords:" + + +def generate_password(length: int, generation_mode_code: int = 0) -> str: + """ + generate_password + + Generate a random password of the specified length by the specified mode. + + Args: + length (int): specify the length of password. + generation_mode_code (int): specify the generation mode of password: (1) Only Number (2) Don't use punctuation (0/default) All ascii character. + + Returns: + str: a password string + """ + password = "".join( + secrets.choice(CharcterSetList[generation_mode_code].value) + for i in range(length) + ) return password + def main(): # Prompt the user for the number of passwords to generate and their length while True: try: - num_pass = int(input("How many passwords do you want to generate? ")) - password_length = int(input("Enter the length of the password(s): ")) + num_pass = int(input(Prompt.GetPasswordInt.value)) + password_length = int(input(Prompt.GetPasswordLength.value)) + generation_mode_code = int(input(Prompt.GetGenerationMode.value)) break except ValueError: - print("Please enter a valid integer.") - continue + print(Prompt.ValueErrorOccured.value) + continue # Generate the specified number of passwords and print them to the console - print("Generated passwords:") + print(Prompt.PasswordGenerated.value) for i in range(num_pass): - password = generate_password(password_length) + password = generate_password(password_length, generation_mode_code) print(f"{i+1}. {password}") + if __name__ == "__main__": main() From eeebbb8e71897d9963c2fe664e8ad8093b2c859c Mon Sep 17 00:00:00 2001 From: Wu Ziban <2314245651@qq.com> Date: Sun, 30 Jun 2024 00:49:38 +0800 Subject: [PATCH 2/2] Update README.md --- PASSWORD RELATED/RandomPassword/README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/PASSWORD RELATED/RandomPassword/README.md b/PASSWORD RELATED/RandomPassword/README.md index aee75158..08b5430d 100644 --- a/PASSWORD RELATED/RandomPassword/README.md +++ b/PASSWORD RELATED/RandomPassword/README.md @@ -3,8 +3,11 @@ Another code snippet where we are going to generate random password which accept * random * string -For instance, when the program runs, it will ask the user to input how many number of password and the length of password to be generated. -to generate a combiantion of letters, numbers and special keys to generate random passwords for us to use +For instance, when the program runs, it will ask the user to input how many number of password , the length of password to be generated and the generation mode code +to generate random passwords for us to use. + +There three generation code, the default code is 0: +(1) Only Number (2) Don't use punctuation (0) All ascii character ### Sample Output: