Skip to content

Commit bb133d0

Browse files
committed
Linted python files
1 parent 72d53e2 commit bb133d0

File tree

3 files changed

+167
-115
lines changed

3 files changed

+167
-115
lines changed

WordBoard.py

Lines changed: 104 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from functools import partial
1212
from WordSearch import WordSearch
1313

14+
1415
class WordBoard:
1516
"""
1617
The WordBoard is the GUI for the WordSearch app. The grid is displayed
@@ -22,7 +23,7 @@ class WordBoard:
2223
LaTeX and String form. This file also contains the solution.
2324
"""
2425

25-
def __init__(self, size=16, color='yellow', file_name='words.txt', words=None):
26+
def __init__(self, size=16, color="yellow", file_name="words.txt", words=None):
2627
"""
2728
Initializes a WordBoard GUI.
2829
@@ -35,16 +36,16 @@ def __init__(self, size=16, color='yellow', file_name='words.txt', words=None):
3536
file_name: File that contains a list of words separated by newline (\n)
3637
characters. Used to select random words for the Word Search.
3738
Default is words.txt. If no file is given or words.txt is not included
38-
in the current directory and words is None, then a FileNotFoundError
39+
in the current directory and words is None, then a FileNotFoundError
3940
is raised.
4041
words: A list of words entered by the user. Allows for customized word searches
4142
with custom words. Default is None. If words is None, then words will be
4243
randomly chosen from words.txt or the file given by file_name.
4344
"""
44-
assert size > 3, 'Size must be greater than 3'
45+
assert size > 3, "Size must be greater than 3"
4546

4647
root = tk.Tk()
47-
root.title('Word Search')
48+
root.title("Word Search")
4849
root.resizable(width=False, height=False)
4950

5051
self._word_grid = tk.Frame(root)
@@ -54,60 +55,72 @@ def __init__(self, size=16, color='yellow', file_name='words.txt', words=None):
5455
self._solution_shown = False
5556
self._size = size
5657
self._color = color
57-
58+
5859
# If file_name is not present in the current directory, the
5960
# New Words button will be disabled.
6061
new_words_button = tk.DISABLED
6162
if file_name in listdir(getcwd()):
6263
new_words_button = tk.NORMAL
63-
with open(file_name, mode='r') as f:
64-
self._wordstxt = filter(None, f.read().split('\n'))
65-
self._wordstxt = list(filter(lambda x: len(x) < self._size - 3, self._wordstxt))
64+
with open(file_name, mode="r") as f:
65+
self._wordstxt = filter(None, f.read().split("\n"))
66+
self._wordstxt = list(
67+
filter(lambda x: len(x) < self._size - 3, self._wordstxt)
68+
)
6669
elif words is None:
67-
raise FileNotFoundError(f'''{file_name} not present in the current directory. {file_name}
68-
must contain words separated by newline (\\n) characters.''')
70+
raise FileNotFoundError(
71+
f"""{file_name} not present in the current directory. {file_name}
72+
must contain words separated by newline (\\n) characters."""
73+
)
6974

7075
# Buttons that have been pushed
7176
self._pushed = set()
72-
77+
7378
self._words = words
7479
if self._words is None:
7580
self._choose_random_words()
7681
else:
7782
self._words = list(set(map(str.upper, self._words)))
78-
83+
7984
# Create empty SIZExSIZE grid of buttons
8085
self._buttons = []
8186
for i in range(self._size):
8287
row = []
8388
for j in range(self._size):
84-
row.append(tk.Button(
85-
self._word_grid, padx=5, command=partial(self._pressed, i, j)
86-
))
87-
row[-1].grid(row=i, column=j, sticky='ew')
89+
row.append(
90+
tk.Button(
91+
self._word_grid, padx=5, command=partial(self._pressed, i, j)
92+
)
93+
)
94+
row[-1].grid(row=i, column=j, sticky="ew")
8895
self._buttons.append(row)
8996

9097
# Menu Buttons at the top right of the GUI
9198
# Menu Label
92-
tk.Label(
93-
self._menu, text='Menu', pady=5, font=tkFont.Font(weight='bold')
94-
).grid(row=0, column=0, columnspan=2, sticky='ew')
99+
tk.Label(self._menu, text="Menu", pady=5, font=tkFont.Font(weight="bold")).grid(
100+
row=0, column=0, columnspan=2, sticky="ew"
101+
)
95102
# "New Words" Button
96103
tk.Button(
97-
self._menu, text='New Words', padx=1, pady=1,
98-
state=new_words_button, command=self._select_new
99-
).grid(row=1, column=0, sticky='ew')
104+
self._menu,
105+
text="New Words",
106+
padx=1,
107+
pady=1,
108+
state=new_words_button,
109+
command=self._select_new,
110+
).grid(row=1, column=0, sticky="ew")
100111
# "Export" Button
101-
self._export_button = tk.Button(self._menu, text='Export', padx=1, pady=1, command=self._export)
102-
self._export_button.grid(row=1, column=1, sticky='ew')
112+
self._export_button = tk.Button(
113+
self._menu, text="Export", padx=1, pady=1, command=self._export
114+
)
115+
self._export_button.grid(row=1, column=1, sticky="ew")
103116
# "Solution" Button
104117
tk.Button(
105-
self._menu, text='Solution', padx=1, pady=1, command=self._solution
106-
).grid(row=2, column=0, sticky='ew')
118+
self._menu, text="Solution", padx=1, pady=1, command=self._solution
119+
).grid(row=2, column=0, sticky="ew")
107120
# "Reshuffle" Button
108121
tk.Button(
109-
self._menu, text='Reshuffle', padx=1, pady=1, command=self._reshuffle
110-
).grid(row=2, column=1, sticky='ew')
122+
self._menu, text="Reshuffle", padx=1, pady=1, command=self._reshuffle
123+
).grid(row=2, column=1, sticky="ew")
111124

112125
self._labels = {}
113126
self._word_search = None
@@ -127,11 +140,17 @@ def _create_labels(self):
127140
for label in self._labels.values():
128141
label.destroy()
129142
self._labels.clear()
130-
self._labels = {'Words': tk.Label(self._word_list, text='Words', pady=5, font=tkFont.Font(weight='bold'))}
131-
self._labels['Words'].grid(row=2, column=0, columnspan=2)
143+
self._labels = {
144+
"Words": tk.Label(
145+
self._word_list, text="Words", pady=5, font=tkFont.Font(weight="bold")
146+
)
147+
}
148+
self._labels["Words"].grid(row=2, column=0, columnspan=2)
132149
for i, word in enumerate(sorted(self._words)):
133-
self._labels[word] = tk.Label(self._word_list, text=word, anchor='w')
134-
self._labels[word].grid(row=(i // 2) + (i % 1) + 3, column=i % 2, sticky='W')
150+
self._labels[word] = tk.Label(self._word_list, text=word, anchor="w")
151+
self._labels[word].grid(
152+
row=(i // 2) + (i % 1) + 3, column=i % 2, sticky="W"
153+
)
135154

136155
def _choose_random_words(self):
137156
"""
@@ -153,12 +172,12 @@ def _pressed(self, row, col):
153172
row, col: The row and column index of the button in the self._buttons
154173
list
155174
"""
156-
if self._buttons[row][col].cget('bg') == self._color:
157-
self._buttons[row][col].configure(bg='SystemButtonFace')
158-
self._pushed.remove((self._buttons[row][col].cget('text'), col, row))
175+
if self._buttons[row][col].cget("bg") == self._color:
176+
self._buttons[row][col].configure(bg="SystemButtonFace")
177+
self._pushed.remove((self._buttons[row][col].cget("text"), col, row))
159178
else:
160179
self._buttons[row][col].configure(bg=self._color)
161-
self._pushed.add((self._buttons[row][col].cget('text'), col, row))
180+
self._pushed.add((self._buttons[row][col].cget("text"), col, row))
162181
for word, coords in self._word_search.solutions.items():
163182
if coords & self._pushed == coords:
164183
for _, col, row in coords:
@@ -172,7 +191,7 @@ def _solution(self):
172191
the words in the board.
173192
"""
174193
if self._solution_shown:
175-
bg = 'SystemButtonFace'
194+
bg = "SystemButtonFace"
176195
state = tk.NORMAL
177196
self._pushed.clear()
178197
else:
@@ -190,7 +209,7 @@ def _reshuffle(self):
190209
Command for the "Reshuffle" button. Uses the existing words and
191210
creates a new word search board with the words in new locations.
192211
"""
193-
self._export_button.configure(text='Export', state=tk.NORMAL)
212+
self._export_button.configure(text="Export", state=tk.NORMAL)
194213

195214
if self._solution_shown:
196215
self._solution_shown = not self._solution_shown
@@ -200,12 +219,13 @@ def _reshuffle(self):
200219
for i in range(self._size):
201220
for j in range(self._size):
202221
self._buttons[i][j].configure(
203-
text=self._word_search.board[i][j], bg='SystemButtonFace',
204-
state=tk.NORMAL
222+
text=self._word_search.board[i][j],
223+
bg="SystemButtonFace",
224+
state=tk.NORMAL,
205225
)
206226

207227
for label in self._labels.values():
208-
label.configure(bg='SystemButtonFace')
228+
label.configure(bg="SystemButtonFace")
209229

210230
def _select_new(self):
211231
"""
@@ -227,54 +247,62 @@ def _export(self):
227247
self._export_button.configure(state=tk.DISABLED)
228248

229249
number = 0
230-
file_name = 'WordSearch.html'
250+
file_name = "WordSearch.html"
231251
while file_name in listdir(getcwd()):
232252
number += 1
233-
file_name = f'WordSearch{number}.html'
234-
235-
with open(file_name, mode='w') as f:
236-
f.write('<!DOCTYPE html>\n')
237-
f.write('<html>\n')
238-
f.write('<head>\n')
239-
f.write('\t<title>Word Search</title>\n')
253+
file_name = f"WordSearch{number}.html"
254+
255+
with open(file_name, mode="w") as f:
256+
f.write("<!DOCTYPE html>\n")
257+
f.write("<html>\n")
258+
f.write("<head>\n")
259+
f.write("\t<title>Word Search</title>\n")
240260
# Scripts required to display LaTeX
241261
f.write(
242-
'''\t<script type="text/x-mathjax-config">
262+
"""\t<script type="text/x-mathjax-config">
243263
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']]}});
244264
</script>
245265
<script type="text/javascript"
246266
src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
247-
</script>\n'''
267+
</script>\n"""
248268
)
249-
f.write('</head>\n')
269+
f.write("</head>\n")
250270
f.write('<h2 align="center">HTML Table WordSearch Grid:</h2>\n<br><br>')
251271

252272
# Create HTML Table Version of the Word Search Grid
253273
f.write('<table align="center">\n')
254274
for i in range(self._size):
255-
f.write('\t<tr>\n\t\t')
275+
f.write("\t<tr>\n\t\t")
256276
for j in range(self._size):
257-
f.write(f'<td padding=1em>{self._word_search.board[i][j]}</td>')
258-
f.write('\t</tr>\n')
259-
f.write('</table>\n<br><br>')
277+
f.write(f"<td padding=1em>{self._word_search.board[i][j]}</td>")
278+
f.write("\t</tr>\n")
279+
f.write("</table>\n<br><br>")
260280

261281
# Create LaTeX Matrix of the Word Search Grid
262282
f.write('<h2 align="center">Latex WordSearch Grid:</h2>\n<br><br>')
263-
f.write('\\begin{matrix}')
264-
f.write(' \\\\ '.join([' & '.join(row) for row in self._word_search.board]))
265-
f.write('\\end{matrix}\n<br><br>')
283+
f.write("\\begin{matrix}")
284+
f.write(" \\\\ ".join([" & ".join(row) for row in self._word_search.board]))
285+
f.write("\\end{matrix}\n<br><br>")
266286
f.write('<h2 align="center">WordSearch Grid as String:</h2>\n<br><br>')
267287

268288
# Create String Version of Word Search Grid. Each Row separated by ':::::'
269289
f.write('<div align="center">\n')
270-
f.write(' ::: '.join([''.join(self._word_search.board[i]) for i in range(self._size)]))
271-
f.write('\n<br><br>\n')
272-
f.write('\n'.join([''.join(self._word_search.board[i]) for i in range(self._size)]))
273-
f.write('</div>\n')
290+
f.write(
291+
" ::: ".join(
292+
["".join(self._word_search.board[i]) for i in range(self._size)]
293+
)
294+
)
295+
f.write("\n<br><br>\n")
296+
f.write(
297+
"\n".join(
298+
["".join(self._word_search.board[i]) for i in range(self._size)]
299+
)
300+
)
301+
f.write("</div>\n")
274302

275303
# Add solution to the bottom of the file
276304
f.write('\n<br><br><h2 align="center">Solution</h2><br><br>\n')
277-
f.write('\\begin{matrix}')
305+
f.write("\\begin{matrix}")
278306

279307
coordinates = set()
280308
for coords in self._word_search.solutions.values():
@@ -288,16 +316,20 @@ def _export(self):
288316
if (self._word_search.board[i][j], j, i) in coordinates:
289317
row.append(self._word_search.board[i][j])
290318
else:
291-
row.append('')
319+
row.append("")
292320
board.append(row)
293-
294-
f.write(' \\\\ '.join([' & '.join(row) for row in board]))
295-
f.write('\\end{matrix}')
321+
322+
f.write(" \\\\ ".join([" & ".join(row) for row in board]))
323+
f.write("\\end{matrix}")
296324

297325
# Add words used in the Word Search and the size of the board
298326
f.write('\n<br><br><h2 align="center">Words</h2><br><br>\n')
299-
f.write(f'''<ul align="center"><li>{'</li><li>'.join(self._words)}</li></ul>\n''')
300-
f.write(f'\n<br><br><h2 align="center">SIZE: {self._size}x{self._size}</h2><br><br>\n')
301-
f.write('</html>')
327+
f.write(
328+
f"""<ul align="center"><li>{'</li><li>'.join(self._words)}</li></ul>\n"""
329+
)
330+
f.write(
331+
f'\n<br><br><h2 align="center">SIZE: {self._size}x{self._size}</h2><br><br>\n'
332+
)
333+
f.write("</html>")
302334

303-
self._export_button.configure(text='Exported')
335+
self._export_button.configure(text="Exported")

0 commit comments

Comments
 (0)