Skip to content

Commit 5fc15d5

Browse files
authored
Merge pull request #30 from IceTheDev2/code-cleaning-separate-logic-and-gui
Code cleaning separate logic and gui
2 parents 4b7a1b9 + 4d1dda6 commit 5fc15d5

File tree

2 files changed

+32
-45
lines changed

2 files changed

+32
-45
lines changed

code/password_strength_gui.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def paste_text() -> None:
3434
keyboard.release(Key.ctrl_l)
3535
keyboard.release('v')
3636

37+
def display_warnings(event) -> None:
38+
for label in labels:
39+
label.configure(text = '')
40+
41+
warnings_list = password_strength_logic.check_password_strength(None, input_box.get())
42+
for index, warning in enumerate(warnings_list):
43+
labels[index].configure(text = warning)
44+
3745
def create_password_strength_frame(frame) -> None:
3846
'''
3947
Called upon starting the program,
@@ -57,7 +65,7 @@ def create_password_strength_frame(frame) -> None:
5765
global input_box
5866
input_box = tk.Entry(frame, width = 32, borderwidth = 2)
5967
input_box.grid(column = 0, row = 2)
60-
input_box.bind('<KeyRelease>', lambda abcdefgh: password_strength_logic.check_password_strength(None, warnings, first_label, input_box.get(), second_label, third_label, fourth_label))
68+
input_box.bind('<KeyRelease>', display_warnings)
6169
input_box.bind('<Button-3>', display_paste_button)
6270

6371
global first_label
@@ -72,8 +80,8 @@ def create_password_strength_frame(frame) -> None:
7280
global fourth_label
7381
fourth_label = tk.Label(frame, font = warning_font, text = '')
7482

75-
global warnings
76-
warnings = [first_label, second_label, third_label, fourth_label]
83+
global labels
84+
labels = [first_label, second_label, third_label, fourth_label]
7785

78-
for label in warnings:
79-
label.grid(column = 0, row = 3 + warnings.index(label))
86+
for label in labels:
87+
label.grid(column = 0, row = 3 + labels.index(label), sticky = 'w')

code/password_strength_logic.py

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
for line in common_passwords_read:
88
modified_common_passwords.append(line.strip()) # Places each of the 100,000 most commonly used passwords into a list
99

10-
def check_password_strength(event, warnings, first_label, inputted_password, second_label, third_label, fourth_label) -> None:
10+
def check_password_strength(event, inputted_password) -> list:
1111
'''
1212
Called upon pressing the done button,
1313
this function hosts all functions necessary to check:
@@ -30,8 +30,6 @@ def check_password_strength(event, warnings, first_label, inputted_password, sec
3030
fourth_label: tkinter.Label()
3131
The fourth warning label.
3232
'''
33-
for label in warnings:
34-
label.configure(text = '')
3533

3634
input = []
3735
input[:0] = inputted_password # Adds each character of the input to a list.
@@ -43,12 +41,10 @@ def check_if_password_is_common() -> None:
4341
checks if the inputted password is in the 100,000 most used passwords (modified_common_password).
4442
'''
4543
if modified_common_passwords.count(inputted_password) > 0:
46-
first_label.configure(text = 'Common: Your password is common.')
47-
first_label.grid(column = 0, row = 3, sticky = 'w')
44+
return 'Common: Your password is common.'
4845

4946
elif modified_common_passwords.count(inputted_password) == 0:
50-
first_label.configure(text = 'Not common: Your password isn\'t common.')
51-
first_label.grid(column = 0, row = 3, sticky = 'w')
47+
return 'Not common: Your password isn\'t common.'
5248

5349
def check_password_length() -> None:
5450
'''
@@ -57,24 +53,19 @@ def check_password_length() -> None:
5753
this function categorises the inputted password as very weak, weak, good, or strong depending on its length.
5854
'''
5955
if len(inputted_password) == 1:
60-
second_label.configure(text = f'Very weak length: Your password has only {str(len(inputted_password))} character.')
61-
second_label.grid(column = 0, row = 4, sticky = 'w')
56+
return f'Very weak length: Your password has only {str(len(inputted_password))} character.'
6257

6358
elif 0 < len(inputted_password) <= 7:
64-
second_label.configure(text = f'Very weak length: Your password has only {str(len(inputted_password))} characters.')
65-
second_label.grid(column = 0, row = 4, sticky = 'w')
59+
return f'Very weak length: Your password has only {str(len(inputted_password))} characters.'
6660

6761
elif 8 <= len(inputted_password) <= 10:
68-
second_label.configure(text = f'Weak length: Your password has only {str(len(inputted_password))} characters.')
69-
second_label.grid(column = 0, row = 4, sticky = 'w')
62+
return f'Weak length: Your password has only {str(len(inputted_password))} characters.'
7063

7164
elif 11 <= len(inputted_password) <= 13:
72-
second_label.configure(text = f'Good length: Your password has {str(len(inputted_password))} characters.')
73-
second_label.grid(column = 0, row = 4, sticky = 'w')
65+
return f'Good length: Your password has {str(len(inputted_password))} characters.'
7466

7567
elif 14 <= len(inputted_password):
76-
second_label.configure(text = f'Strong length: Your password has {str(len(inputted_password))} characters.')
77-
second_label.grid(column = 0, row = 4, sticky = 'w')
68+
return f'Strong length: Your password has {str(len(inputted_password))} characters.'
7869

7970
def check_password_complexity() -> None:
8071
'''
@@ -134,12 +125,10 @@ def check_password_complexity() -> None:
134125
else:
135126
output = output + 'and ' + str(missing_feature)
136127

137-
third_label.configure(text = f'Not complex: Your password is missing {output}.')
138-
third_label.grid(column = 0, row = 5, sticky = 'w')
128+
return f'Not complex: Your password is missing {output}.'
139129

140130
else:
141-
third_label.configure(text = 'Complex: Your password contains lowercase letters, uppercase letters, digits, and punctation.')
142-
third_label.grid(column = 0, row = 5, sticky = 'w')
131+
return 'Complex: Your password contains lowercase letters, uppercase letters, digits, and punctation.'
143132

144133
def check_for_patterns_in_password() -> None:
145134
'''
@@ -151,16 +140,6 @@ def check_for_patterns_in_password() -> None:
151140
global are_there_repeated_characters
152141
are_there_repeated_characters = False
153142

154-
def show_repeated_pattern_warning() -> None:
155-
'''
156-
Called by the check_for_patterns_in_password_function
157-
(upon pressing the done button),
158-
this function warns the user if there are repeated characters in their password.
159-
'''
160-
161-
fourth_label.configure(text = 'Repeated character(s): Your password contains at least one repeated character.')
162-
fourth_label.grid(column = 0, row = 6, sticky = 'w')
163-
164143
for character in input: # For every character in the input, it checks if there is another character that matches, and shows the repeated pattern warning if there is.
165144
count = 0
166145
for other_character in input:
@@ -169,17 +148,17 @@ def show_repeated_pattern_warning() -> None:
169148

170149
if count > 1:
171150
are_there_repeated_characters = True
172-
show_repeated_pattern_warning()
151+
return 'Repeated character(s): Your password contains at least one repeated character.'
173152

174153
if are_there_repeated_characters == False:
175-
fourth_label.configure(text = 'No repeated characters: Your password contains no repeated characters.')
176-
fourth_label.grid(column = 0, row = 6, sticky = 'w')
154+
return 'No repeated characters: Your password contains no repeated characters.'
177155

178156
if len(inputted_password) == 0:
179-
first_label.configure(text = 'Please input a password.')
180-
first_label.grid(column = 0, row = 3, sticky = 'n')
157+
return 'Please input a password.'
181158
else:
182-
check_if_password_is_common()
183-
check_password_length()
184-
check_password_complexity()
185-
check_for_patterns_in_password()
159+
prevalance_warning = check_if_password_is_common()
160+
length_warning = check_password_length()
161+
complexity_warning = check_password_complexity()
162+
pattern_warning = check_for_patterns_in_password()
163+
164+
return [prevalance_warning, length_warning, complexity_warning, pattern_warning]

0 commit comments

Comments
 (0)