I have made a random password generator using a class called password
and a method called generate
.
My program works as it should. It generates a random password determined by the users preferences for length, upper or lowercase, numbers and special characters.
I was just wondering if there was a way to refactor the numerous if statements
I have used to determine what sort of password the program would generate.
Any other suggestions for improvements I could make would also be helpful. Thanks a ton :D
Code:
JavaScript
x
50
50
1
import random
2
import string
3
4
class password:
5
def __init__(self, length, string_method, numbers=True, special_chars=False):
6
self.length = length
7
self.string_method = string_method
8
self.numbers = numbers
9
self.special_chars = special_chars
10
11
def generate(self, iterations):
12
13
# Checking what type of string method the user has asked for
14
15
if self.string_method == 'upper':
16
stringMethod = string.ascii_uppercase
17
elif self.string_method == 'lower':
18
stringMethod = string.ascii_lowercase
19
elif self.string_method == 'both':
20
stringMethod = string.ascii_letters
21
22
# Checking if the user has asked for numbers or not
23
24
if self.numbers == True:
25
stringNumbers = string.digits
26
elif self.numbers == False:
27
stringNumbers = ''
28
29
# Checking if the user has asked for special characters or not
30
31
if self.special_chars == True:
32
stringSpecial = string.punctuation
33
elif self.special_chars == False:
34
stringSpecial = ''
35
36
characters = stringMethod + stringNumbers + stringSpecial
37
38
# Generating the password
39
40
for p in range(iterations):
41
output_password = ''
42
for c in range(self.length):
43
output_password += random.choice(characters)
44
print(output_password)
45
46
# Test
47
48
password1 = password(20, 'lower', True, False) # password length = 20, string method is lowercase, numbers are true and special characters are false
49
password1.generate(3) # generate the random password 3 times```
50
Advertisement
Answer
Evaluate it in the initializer. It’s not less if checks per se, but it’s a bit cleaner.
JavaScript
1
29
29
1
import random
2
import string
3
4
class Password:
5
def __init__(self, length, string_method, numbers=True, special_chars=False):
6
self.length = length
7
self.string_method = {
8
'upper': string.ascii_uppercase,
9
'lower': string.ascii_lowercase,
10
'both': string.ascii_letters
11
}[string_method]
12
self.numbers = string.digits if numbers else ''
13
self.special_chars = string.punctuation if special_chars else ''
14
15
def generate(self, iterations):
16
characters = self.string_method + self.numbers + self.special_chars
17
18
# Generating the password
19
for p in range(iterations):
20
output_password = ''
21
for c in range(self.length):
22
output_password += random.choice(characters)
23
print(output_password)
24
25
# Test
26
27
password = Password(20, 'lower', True, False)
28
password.generate(3) # generate the random password 3 times
29
If you actually want less checks, then just pass in the characters directly.
JavaScript
1
21
21
1
import random
2
import string
3
4
class Password:
5
def __init__(self, length, characters):
6
self.length = length
7
self.characters = characters
8
9
def generate(self, iterations):
10
# Generating the password
11
for p in range(iterations):
12
output_password = ''
13
for c in range(self.length):
14
output_password += random.choice(self.characters)
15
print(output_password)
16
17
# Test
18
19
password = Password(20, string.ascii_lowercase + string.digits)
20
password.generate(3) # generate the random password 3 times
21