Skip to content

Commit 881438b

Browse files
committed
fix: baseException and fixing unused imported modules
1 parent e8fb181 commit 881438b

File tree

9 files changed

+51
-40
lines changed

9 files changed

+51
-40
lines changed

CountMillionCharacter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@
301301
try:
302302
if special_character.group(): #returns all matching groups
303303
wordlist[x] = y[:-1]
304-
except:
304+
except BaseException:
305305
continue
306306

307307
wordfreq = [wordlist.count(w) for w in wordlist] #counts frequency of a letter in the given list

Flappy Bird - created with tkinter/Bird.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def checkCollision(self):
102102
for _id in ignored_collisions:
103103
try:
104104
possible_collisions.remove(_id)
105-
except:
105+
except BaseException:
106106
continue
107107

108108
# Se houver alguma colisão o pássaro morre

Flappy Bird - created with tkinter/Flappy Bird.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def loadScore(self):
305305
file.close()
306306

307307
# Se não for possível, será criado um arquivo para guardar o placar
308-
except:
308+
except BaseException:
309309
file = open(self.score_fp, 'w')
310310
file.write(bin(self.__bestScore))
311311
file.close()

Flappy Bird - created with tkinter/Settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def setOptions(self):
8585
setattr(Settings, attr, data[attr])
8686

8787
# Caso não exista um arquivo para obter as configurações, ele será criado
88-
except:
88+
except BaseException:
8989

9090
# Caso não exista o diretório, o mesmo será criado.
9191
if not os.path.exists(os.path.split(self.settings_fp)[0]):

Flappy Bird - created with tkinter/Tubes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, background, bird, score_function=None, *screen_geometry, fp=(
5050
# Cria uma lista para guardar imagens dos tubos
5151
try:
5252
self.deleteAll()
53-
except:
53+
except BaseException:
5454
self.__background.tubeImages = []
5555

5656
# Cria uma lista somente para guardar as imagens futuras dos corpos dos tubos gerados

binary_search_tree.py

+37-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import sys
2-
31
class Node:
42
"""Class for node of a tree"""
3+
54
def __init__(self, info):
65
"""Initialising a node"""
76
self.info = info
@@ -18,6 +17,7 @@ def __del__(self):
1817

1918
class BinarySearchTree:
2019
"""Class for BST"""
20+
2121
def __init__(self):
2222
"""Initialising a BST"""
2323
self.root = None
@@ -46,7 +46,7 @@ def insert(self, val):
4646
else:
4747
break
4848

49-
def search(self, val, to_delete = False):
49+
def search(self, val, to_delete=False):
5050
current = self.root
5151
prev = -1
5252
while current:
@@ -58,12 +58,12 @@ def search(self, val, to_delete = False):
5858
current = current.right
5959
elif current.info == val:
6060
if not to_delete:
61-
return 'Match Found'
61+
return "Match Found"
6262
return prev
6363
else:
6464
break
6565
if not to_delete:
66-
return 'Not Found'
66+
return "Not Found"
6767

6868
# Method to delete a tree-node if it exists, else error message will be returned.
6969
def delete(self, val):
@@ -83,21 +83,21 @@ def delete(self, val):
8383
else:
8484
prev2.right = None
8585
self.root.info = temp.info
86-
print('Deleted Root ', val)
86+
print("Deleted Root ", val)
8787
# Check if node is to left of its parent
8888
elif prev.left and prev.left.info == val:
8989
# Check if node is leaf node
9090
if prev.left.left is prev.left.right:
9191
prev.left = None
92-
print('Deleted Node ', val)
92+
print("Deleted Node ", val)
9393
# Check if node has child at left and None at right
9494
elif prev.left.left and prev.left.right is None:
9595
prev.left = prev.left.left
96-
print('Deleted Node ', val)
96+
print("Deleted Node ", val)
9797
# Check if node has child at right and None at left
9898
elif prev.left.left is None and prev.left.right:
9999
prev.left = prev.left.right
100-
print('Deleted Node ', val)
100+
print("Deleted Node ", val)
101101
# Here node to be deleted has 2 children
102102
elif prev.left.left and prev.left.right:
103103
temp = prev.left
@@ -106,10 +106,9 @@ def delete(self, val):
106106
temp = temp.right
107107
prev2.right = None
108108
prev.left.info = temp.info
109-
print('Deleted Node ', val)
109+
print("Deleted Node ", val)
110110
else:
111-
print('Error Left')
112-
111+
print("Error Left")
113112

114113
# Check if node is to right of its parent
115114
elif prev.right.info == val:
@@ -118,31 +117,32 @@ def delete(self, val):
118117
if prev.right.left is prev.right.right:
119118
prev.right = None
120119
flag = 1
121-
print('Deleted Node ', val)
120+
print("Deleted Node ", val)
122121
# Check if node has left child at None at right
123122
if prev.right and prev.right.left and prev.right.right is None:
124123
prev.right = prev.right.left
125-
print('Deleted Node ', val)
124+
print("Deleted Node ", val)
126125
# Check if node has right child at None at left
127126
elif prev.right and prev.right.left is None and prev.right.right:
128127
prev.right = prev.right.right
129-
print('Deleted Node ', val)
128+
print("Deleted Node ", val)
130129
elif prev.right and prev.right.left and prev.right.right:
131130
temp = prev.right
132131
while temp.left is not None:
133132
prev2 = temp
134133
temp = temp.left
135134
prev2.left = None
136135
prev.right.info = temp.info
137-
print('Deleted Node ', val)
136+
print("Deleted Node ", val)
138137
else:
139138
if flag == 0:
140139
print("Error")
141140
else:
142141
print("Node doesn't exists")
143142

144143
def __str__(self):
145-
return 'Not able to print tree yet'
144+
return "Not able to print tree yet"
145+
146146

147147
def is_bst(node, lower_lim=None, upper_lim=None):
148148
"""Function to find is a binary tree is a binary search tree."""
@@ -158,6 +158,7 @@ def is_bst(node, lower_lim=None, upper_lim=None):
158158
is_right_bst = is_bst(node.right, node.info, upper_lim)
159159
return is_left_bst and is_right_bst
160160

161+
161162
def postorder(node):
162163
# L R N : Left , Right, Node
163164
if node is None:
@@ -179,6 +180,7 @@ def inorder(node):
179180
if node.right:
180181
inorder(node.right)
181182

183+
182184
def preorder(node):
183185
# N L R : Node , Left, Right
184186
if node is None:
@@ -189,6 +191,7 @@ def preorder(node):
189191
if node.right:
190192
preorder(node.right)
191193

194+
192195
# Levelwise
193196
def bfs(node):
194197
queue = []
@@ -202,6 +205,7 @@ def bfs(node):
202205
if temp.right:
203206
queue.append(temp.right)
204207

208+
205209
def preorder_itr(node):
206210
# N L R : Node, Left , Right
207211
stack = [node]
@@ -216,29 +220,31 @@ def preorder_itr(node):
216220
stack.append(temp.left)
217221
return values
218222

223+
219224
def inorder_itr(node):
220225
# L N R : Left, Node, Right
221226
# 1) Create an empty stack S.
222227
# 2) Initialize current node as root
223228
# 3) Push the current node to S and set current = current->left until current is NULL
224-
# 4) If current is NULL and stack is not empty then
229+
# 4) If current is NULL and stack is not empty then
225230
# a) Pop the top item from stack.
226-
# b) Print the popped item, set current = popped_item->right
231+
# b) Print the popped item, set current = popped_item->right
227232
# c) Go to step 3.
228233
# 5) If current is NULL and stack is empty then we are done.
229234
stack = []
230235
current = node
231236
while True:
232237
if current != None:
233-
stack.append(current) # L
238+
stack.append(current) # L
234239
current = current.left
235240
elif stack != []:
236241
temp = stack.pop()
237-
print(temp.info) # N
238-
current = temp.right # R
242+
print(temp.info) # N
243+
current = temp.right # R
239244
else:
240245
break
241246

247+
242248
def postorder_itr(node):
243249
# L R N
244250
# 1. Push root to first stack.
@@ -256,6 +262,7 @@ def postorder_itr(node):
256262
s1.append(temp.right)
257263
print(*(s2[::-1]))
258264

265+
259266
def bst_frm_pre(pre_list):
260267
box = Node(pre_list[0])
261268
if len(pre_list) > 1:
@@ -272,11 +279,12 @@ def bst_frm_pre(pre_list):
272279
else:
273280
all_less = True
274281
if i != 1:
275-
box.left = bst_frm_pre(pre_list[1 : i])
282+
box.left = bst_frm_pre(pre_list[1:i])
276283
if not all_less:
277284
box.right = bst_frm_pre(pre_list[i:])
278285
return box
279286

287+
280288
# Function to find the lowest common ancestor of nodes with values c1 and c2.
281289
# It return value in the lowest common ancestor, -1 indicates value returned for None.
282290
# Note that both values v1 and v2 should be present in the bst.
@@ -293,9 +301,10 @@ def lca(t_node, c1, c2):
293301
return current.info
294302
return -1
295303

304+
296305
# Function to print element vertically which lie just below the root node
297306
def vertical_middle_level(t_node):
298-
e = (t_node, 0) # 0 indicates level 0, to left we have -ve and to right +ve
307+
e = (t_node, 0) # 0 indicates level 0, to left we have -ve and to right +ve
299308
queue = [e]
300309
ans = []
301310
# Do a level-order traversal and assign level-value to each node
@@ -307,7 +316,8 @@ def vertical_middle_level(t_node):
307316
queue.append((temp.left, level - 1))
308317
if temp.right:
309318
queue.append((temp.right, level + 1))
310-
return ' '.join(ans)
319+
return " ".join(ans)
320+
311321

312322
def get_level(n, val):
313323
c_level = 0
@@ -319,10 +329,11 @@ def get_level(n, val):
319329
n = n.right
320330
c_level += 1
321331
if n is None:
322-
return -1
332+
return -1
323333

324334
return c_level
325335

336+
326337
def depth(node):
327338
if node is None:
328339
return 0

calculator.py

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"""
2525

2626
import sys
27-
import math
2827
## Imported math library to run sin(), cos(), tan() and other such functions in the calculator
2928

3029
from fileinfo import raw_input

check_internet_con.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from sys import argv
2+
23
try:
34
# For Python 3.0 and later
45
from urllib.error import URLError
@@ -11,14 +12,14 @@
1112
def checkInternetConnectivity():
1213
try:
1314
url = argv[1]
14-
if 'https://' or 'http://' not in url:
15-
url = 'https://' + url
16-
except:
17-
url = 'https://google.com'
15+
if "https://" or "http://" not in url:
16+
url = "https://" + url
17+
except BaseException:
18+
url = "https://google.com"
1819
try:
19-
urlopen(url, timeout=2)
20-
print("Connection to \""+ url + "\" is working")
21-
20+
urlopen(url, timeout=2)
21+
print(f'Connection to "{url}" is working')
22+
2223
except URLError as E:
2324
print("Connection error:%s" % E.reason)
2425

fibonacci.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def getFibonacciIterative(n: int) -> int:
1212
a = 0
1313
b = 1
1414

15-
for i in range(n):
15+
for _ in range(n):
1616
a, b = b, a + b
1717

1818
return a

0 commit comments

Comments
 (0)